Ask a question about using macros

Why #[macro_use] attribute macros cannot be used on use crate

like:

#[macro_use]
pub use crate::web2::macros::cfg;

Compiler warns:

#[macro_use]
Valid forms are:

#[macro_use]
#[macro_use(name1, name2, ...)]
`#[macro_use]` only has an effect on `extern crate` and modules
`#[warn(unused_attributes)]` on by defaultrustc(unused_attributes)

I mean the error message says exactly why - they have to be declared on extern crate ... items. Anywhere else, they have no meaning.

2 Likes

I want to share use macros under multiple internal folders

This syntax is used to import all macros from a crate globally.

// in main.rs or lib.rs
#[macro_use]
extern crate foo;

// can be used anywhere in your crate
my_macro!()

Otherwise you have to import the macro with use in each file you need it.

use foo;

foo::my_macro!()
4 Likes

thanks, i try

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.