How to use macros in multiple Rust module files?
Hello! I'm learning Rust and ran into a problem when trying to move macros into separate modules. I have a project where I defined a useful macro `my_println!` in `main.rs`. I want to use it in other modules that are in separate files.
Here's my project structure:
```
src/
main.rs
utils/
mod.rs
helper.rs
```
In `main.rs` I defined the macro:
```rust
macro_rules! my_println {
($($arg:tt)*) => {
println!("[LOG] {}", format!($($arg)*));
};
}
mod utils;
```
In `utils/mod.rs` I declared the submodule:
```rust
pub mod helper;
```
In `utils/helper.rs` I'm trying to use the macro:
```rust
pub fn do_something() {
my_println!("Hello from helper!");
}
```
But the compiler gives an error: `error: cannot find macro 'my_print
No answers yet.
Log in to leave an answer
Blogs