Question about macro import

I have defined macro like

#[allow(missing_docs)]
#[macro_export]
#[warn(unused_macros)]
macro_rules! some_macro {
//Some stuff
}

and imported it in some file.rs in the same directory and on parent(root), here pops up a problem

error[E0428]: the name `some_macro` is defined multiple times

`some_macro` redefined here
    previous definition of the macro `some_macro` here

How I need to define and import in whatever .rs files would like, thanks!)

That's covered by little book.

Macros scoping is different from all other objects, you don't need to import it when they are used in the same crate.

1 Like

I had read, but there I didn't found usual approach to make macro with macro export and use it in whatever file inside crate or workspace) Or they needed to be put in mod.rs?)

The usual approach is to put module with macros at the top of mod.rs (like you would do in C or C++) and only use #[macro_export] from other crates.

Okay friend, also interesting: is there new macro rule as in this book or this will be only soon?) As you think, is there sense to use it now or wait until stable verion of new macro(as it will be changed soon)?)
Also had forgotten, if I write

pub(crate) use tokio::*;

for example in src/some_subdirectory/sub_sub_/file.rs will tokio be visible in src/main.rs?)
Or pub(crate) doesn't affect like this with extern crates?)

Special rules only apply to macros defined in your crate.

If you import them they behave like other objects.

I mean not only macros, functions etc. they wouldn't be imported as like my own functions in crate as I understood?) These pub, pub(crate) neeeded only for my definitions?)
Thanks)

They would behave like your own functions defined in that exact point.

They don't affect your module, they only are meaningful if these types must be reexported.

pub(crate) means they are only accessible in other modules in your own crate, bu you would still need to import them.

Only macro_rules defined in your crate are special.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.