# Am I doing something wrong that makes macros so hard to include?

I have question about use statement with macros. Let me start by explaining my code.

I created a macro into its own file, macro.rs:

``````use dates::now_date_time_str;
// the above line creates a warning:
// warning: unused import: `dates::now_date_time_str`

macro_rules! debugln {
(\$exp : expr) =>
{
let local = now_date_time_str();
println!("{} -> {}", local, \$exp);
};
() =>
{
println!("");
};
}
``````

The macro makes use of function defined in dates.rs. I haven’t included that code here, thinking its not relevant but I can add it if needed.

I then consumed this macro in main.rs like this:

``````extern crate chrono;

mod dates;

#[macro_use]
mod macros;

use dates::now_date_time_str;
// if I dont include the above, I get the following error!!!
// error[E0425]: cannot find function `now_date_time_str` in this scope

fn main() {
debugln!("start of app");
}
``````

I understand macros are expanded inline. What I do not understand is why the use statement in macro.rs is “unused” and not applied with “macro” mod is included in main.rs.

It seems a little cumbersome to create macros if the consumer has to know which uses statements to include in its code. Am I doing something wrong that makes macros so hard to include?

Thnx
Matt

2 Likes

I think you should replace `let local = now_date_time_str();` with `let local = \$crate::dates::now_date_time_str();`. Then you don’t need to import `dates` when using the macro (caveat: I’m not an expert on macros).

3 Likes

thank you. that addresses my concerns.

To explain the problem here: names are resolved relative to where macros are expanded, not where they are defined. As such, all paths in a macro should be absolute, to ensure they’ll correctly resolve no matter where the macro is used (unless the macro is only ever used in its defining module).

6 Likes