About macro_use and macro_export

Hi guys,
I found I can macro_use a macro without marking it with macro_export, this does not match the description in this doc. the following code can compile. Any suggestion?

mod my {
  macro_rules! mym {
    () => {println!("my macro")}

fn main() {

The doc is right. There are two cases: in the defined crate, and in the outer crate. Read the doc carefully.

I think you mean this statement:

Macros to be imported with #[macro_use] must be exported with #[macro_export], which is described below.

The statement applies to importing macros from extern crate sources, like:

extern crate lazy_static;

which you nowadays (since edition 2018) don't really need to care about. This does not apply to the #[macro_use] attribute declared on modules, like you did in your example. Applied to a module, it extends the scope of the macros contained in that module. It basically allows the macros to "escape" their module into the parent module. Hence you can call the macros in your parent module, without any need for #[macro_export].

1 Like

My bad. This sentence is in the "extern crate" paragraph.

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.