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,

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);
    () =>

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

I then consumed this macro in like this:

extern crate chrono;

mod dates;

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 is “unused” and not applied with “macro” mod is included in

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?



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).


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).