188

I have two modules in separate files within the same crate, where the crate has macro_rules enabled. I want to use the macros defined in one module in another module.

// macros.rs
#[macro_export] // or not? is ineffectual for this, afaik
macro_rules! my_macro(...)

// something.rs
use macros;
// use macros::my_macro; <-- unresolved import (for obvious reasons)
my_macro!() // <-- how?

I currently hit the compiler error "macro undefined: 'my_macro'"... which makes sense; the macro system runs before the module system. How do I work around that?

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
user
  • 4,920
  • 3
  • 25
  • 38

5 Answers5

285

Macros within the same crate

New method (since Rust 1.32, 2019-01-17)

foo::bar!();  // works

mod foo {
    macro_rules! bar {
        () => ()
    }

    pub(crate) use bar;    // <-- the trick
}

foo::bar!();  // works

With the pub use, the macro can be used and imported like any other item. And unlike the older method, this does not rely on source code order, so you can use the macro before (source code order) it has been defined.

Old method

bar!();   // Does not work! Relies on source code order!

#[macro_use]
mod foo {
    macro_rules! bar {
        () => ()
    }
}

bar!();    // works

If you want to use the macro in the same crate, the module your macro is defined in needs the attribute #[macro_use]. Note that macros can only be used after they have been defined!



Macros across crates

Crate util

#[macro_export]
macro_rules! foo {
    () => ()
}

Crate user

use util::foo;

foo!();

Note that with this method, macros always live at the top-level of a crate! So even if foo would be inside a mod bar {}, the user crate would still have to write use util::foo; and not use util::bar::foo;. By using pub use, you can export a macro from a module of your crate (in addition to it being exported at the root).

Before Rust 2018, you had to import macro from other crates by adding the attribute #[macro_use] to the extern crate util; statement. That would import all macros from util. This syntax should not be necessary anymore.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
  • 59
    "Macros can only be used after they have been defined." - This is key because you can run into that error even when you've done all the other things mentioned correctly. For example, if you have modules `macros` and `foo` (which uses a macro from `macros`), and you list them in alphabetical order in your lib.rs or main.rs, foo will be loaded before macros and the code won't compile. – neverfox Aug 15 '17 at 02:55
  • 14
    ^ pro tip - this totally got me – semore_1267 Oct 18 '18 at 00:55
  • 8
    Also note that for using macros internally, the `#[macro_use]` attribute should be on every module and parent module, etc.. until it reaches the point where you need to use it. – Ten Mar 02 '20 at 15:42
  • 2
    This answer did not work for me. The module that declared the macro had `#[macro_use]` and it was declared first in lib.rs - still didn't work. @Ten's answer helped and I added `#[macro_use]` to the top of lib.rs - then it worked. But I'm still not sure what the best practice is since i read [here](https://www.reddit.com/r/rust/comments/2lc84e/importing_macros_from_another_module/) that "You don't import macros from other modules; you export the macro from the defining module" – Sorin Bolos Sep 10 '20 at 18:04
  • 6
    I always forget how Rust's macros work with modules. It's an awful system, and hopefully there'll be a better one someday. – Hutch Moore Nov 09 '20 at 20:05
  • @Ten 's hint helped me. `#[macro_use]` needs to be added to all `mod` statements on the complete path, in order to be usable by parent modules. That's very crazy. Thank you so much! – Aitch Dec 15 '20 at 23:31
  • 1
    It's very counter intuitive. So we define macros in a sub module, but then call it from the root crate? Not great for code organization either. – Randall Coding Jun 10 '21 at 15:46
  • I agree with @RandallCoding Can we not put macros at root of crate when using `#[macro_export]`? Sometimes we want to use macros across crates, but we may not always want to export them at root of crate when they are defined in submodules. We want to `pub use` by ourselves. – TSK Jan 03 '23 at 09:18
62

Alternative approach as of 1.32.0 (2018 edition)

Note that while the instructions from @lukas-kalbertodt are still up to date and work well, the idea of having to remember special namespacing rules for macros can be annoying for some people.

On the 2018 edition and onwards, since the version 1.32.0 of Rust, there is another approach which works as well, and which has the benefit, imho, of making it easier to teach (e.g., it renders #[macro_use] obsolete). The key idea is the following:

A re-exported macro behaves as any other item (function, type, constant, etc.): it is namespaced within the module where the re-export occurs.

  • It can then be referred to with a fully qualified path.

  • It can also be locally used / brought into scope so as to refer to it in an unqualified fashion.

Example

macro_rules! macro_name { ... }
pub(crate) use macro_name; // Now classic paths Just Work™

And that's it. Quite simple, huh?


Feel free to keep reading, but only if you are not scared of information overload ;) I'll try to detail why, how and when exactly does this work.

More detailed explanation

In order to re-export (pub(...) use ...) a macro, we need to refer to it! That's where the rules from the original answer are useful: a macro can always be named within the very module where the macro definition occurs, but only after that definition.

macro_rules! my_macro { ... }
my_macro!(...); // OK
// Not OK
my_macro!(...); /* Error, no `my_macro` in scope! */
macro_rules! my_macro { ... }

Based on that, we can re-export a macro after the definition; the re-exported name, then, in and of itself, is location agnostic, as all the other global items in Rust

  • In the same fashion that we can do:

    struct Foo {}
    
    fn main() {
        let _: Foo;
    }
    
  • We can also do:

    fn main() {
        let _: A;
    }
    
    struct Foo {}
    use Foo as A;
    
  • The same applies to other items, such as functions, but also to macros!

    fn main() {
        a!();
    }
    
    macro_rules! foo { ... } // foo is only nameable *from now on*
    use foo as a;            // but `a` is now visible all around the module scope!
    

    And it turns out that we can write use foo as foo;, or the common use foo; shorthand, and it still works.

The only question remaining is: pub(crate) or pub?

  • For #[macro_export]-ed macros, you can use whatever privacy you want; usually pub.

  • For the other macro_rules! macros, you cannot go above pub(crate).


Detailed examples

  • For a non-#[macro_export]ed macro

    mod foo {
        use super::example::my_macro;
    
        my_macro!(...); // OK
    }
    
    mod example {
        macro_rules! my_macro { ... }
        pub(crate) use my_macro;
    }
    
    example::my_macro!(...); // OK
    
  • For a #[macro_export]-ed macro

    Applying #[macro_export] on a macro definition makes it visible after the very module where it is defined (so as to be consistent with the behavior of non-#[macro_export]ed macros), but it also puts the macro at the root of the crate (where the macro is defined), in an absolute path fashion.

    This means that a pub use macro_name; right after the macro definition, or a pub use crate::macro_name; in any module of that crate will work.

    • Note: in order for the re-export not to collide with the "exported at the root of the crate" mechanic, it cannot be done at the root of the crate itself.
    pub mod example {
        #[macro_export] // macro nameable at `crate::my_macro`
        macro_rules! my_macro { ... }
        pub use my_macro; // macro nameable at `crate::example::my_macro`
    }
    
    pub mod foo {
        pub use crate::my_macro; // macro nameable at `crate::foo::my_macro`
    }
    

When using the pub / pub(crate) use macro_name;, be aware that given how namespaces work in Rust, you may also be re-exporting constants / functions or types / modules. This also causes problems with globally available macros such as #[test], #[allow(...)], #[warn(...)], etc.

In order to solve these issues, remember you can rename an item when re-exporting it:

macro_rules! __test__ { ... }
pub(crate) use __test__ as test; // OK

macro_rules! __warn__ { ... }
pub(crate) use __warn__ as warn; // OK

Also, some false positive lints may fire:

Daniel H-M
  • 1,439
  • 8
  • 12
  • 1
    I think this is the only way to use use a local macro from a module that isn’t directly above the module which defines the macro (for example if you had a macro in `a::b` this is the only way to use it in module `c` without `#[macro_export]`). This is because `c` can’t declare `mod a::b` and `#[macro_use]` doesn’t work with `use` statements such as `use super::a::b`. – Lauren Yim Apr 17 '21 at 16:47
  • 1
    Oh wow, this answer needs more upvotes. Many thanks! – Zeyi Fan Apr 30 '21 at 23:27
  • 1
    *A re-exported macro behaves as any other item* - Based on this, I'd expect to be able to define a macro in a (public) sub-module of a crate, and refer to it from another crate using full path, i.e. as `some_name::some_module::some_macro!`. But if I define `macro_rules! some_macro { ... }` and then `pub use some_macro;` the compiler tells me that `some_macro` is private. I can use `pub(crate)` as shown in the answer, but then it's private to the crate, and only callable using full path from that crate. Is there a way to call it by full path from another crate? – user4815162342 May 06 '21 at 10:04
  • @user4815162342 a re-export cannot provide more visibility than the inherent one of an item. And a non-`#[macro_export]`-ed macro inherent visibility is indeed `pub(crate)` at most. You'll thus need to `#[macro_export]` your macro, although that will make it appear at the root of the crate too. There is no simple workaround for that "also at the root of the crate" issue, but for doc hacks to hide it, or for using an extra external helper crate just for that (such as in `konst_macro_rules` in https://crates.io/crates/konst/0.2.4/dependencies) – Daniel H-M May 07 '21 at 13:51
  • Ok, so I can use `#[macro_export] macro_rules some_macro { ... }` followed by `pub use some_macro`, and I'll get `some_macro` both in crate root and the module where I want it. While I'd prefer to have it only in the submodule (because the macro is grouped with types and other stuff from that submodule in a logical whole), this works too. I don't really understand why a macro has to be exported to crate root and why that export cannot be disabled (while still allowing public export in other locations). Do you know the rationale for that design? – user4815162342 May 07 '21 at 14:15
  • Historical remains, I'd say: originally macros were not "namespaceable" whatsoever, which kind of worked for a crate-local macro, but a special mechanism had to be designed for cross-crate macros. Given their lack of namespace, cross-crate macros, that is, `#[macro_export]`-ed ones, were thus arbitrarily given the "at the root of the crate" namespace / module location. This wouldn't be a problem if the macro could be `#[doc(hidden)]` and then `#[doc(inline)] pub use`d, but then a rustdoc bug makes `doc(hidden)` transitive across re-exports – Daniel H-M May 09 '21 at 12:21
  • 2
    Finally. This was the answer I was a looking for. If I define a macro in a module I want it namespaced to that module. This has to be the most confusing and poorly documented feature of rust, macros in general. – Randall Coding Jun 10 '21 at 15:58
26

This answer is outdated as of Rust 1.1.0-stable.


You need to add #![macro_escape] at the top of macros.rs and include it using mod macros; as mentioned in the Macros Guide.

$ cat macros.rs
#![macro_escape]

#[macro_export]
macro_rules! my_macro {
    () => { println!("hi"); }
}

$ cat something.rs
#![feature(macro_rules)]
mod macros;

fn main() {
    my_macro!();
}

$ rustc something.rs
$ ./something
hi

For future reference,

$ rustc -v
rustc 0.13.0-dev (2790505c1 2014-11-03 14:17:26 +0000)
ideasman42
  • 42,413
  • 44
  • 197
  • 320
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • I'd totally missed that attribute. Thanks! – user Nov 04 '14 at 09:21
  • 5
    BTW, `#[macro_export]` attribute is unnecessary here. It is only needed if the macro should be exported to external crate users. If the macro is only used inside the crate, `#[macro_export]` is not needed. – Vladimir Matveev Nov 04 '14 at 10:30
  • 1
    Thanks a lot for the answer. I just want to add, that if your `something.rs` file uses other modules, for example with `mod foobar;`, and this `foobar` module uses the macros from `macro.rs`, then you have to put `mod macro;` *before* `mod foobar;` for the program to compile. Minor thing, but this is not an obvious IMO. – conradkleinespel Feb 06 '15 at 13:26
  • 3
    (n.b. this answer is now outdated; I've accepted the up-to-date answer given by Lukas) – user Aug 01 '15 at 12:06
12

Adding #![macro_use] to the top of your file containing macros will cause all macros to be pulled into main.rs.

For example, let's assume this file is called node.rs:

#![macro_use]

macro_rules! test {
    () => { println!("Nuts"); }
}

macro_rules! best {
    () => { println!("Run"); }
}

pub fn fun_times() {
    println!("Is it really?");
}

Your main.rs would look sometime like the following:

mod node;  //We're using node.rs
mod toad;  //Also using toad.rs

fn main() {
    test!();
    best!();
    toad::a_thing();
}

Finally let's say you have a file called toad.rs that also requires these macros:

use node; //Notice this is 'use' not 'mod'

pub fn a_thing() {
  test!();

  node::fun_times();
}

Notice that once files are pulled into main.rs with mod, the rest of your files have access to them through the use keyword.

Luke Dupin
  • 2,275
  • 23
  • 30
  • I added more clarification. As of rustc 1.22.1, this does work. – Luke Dupin Jan 16 '18 at 17:38
  • Are you sure? Where is this #![macro_use] (not #[macro_use]) documented? I can't find it. It doesn't work here. – Markus Oct 27 '18 at 17:12
  • This worked when I posted it, Rust's include system is such a awful mess, its entirely possible this doesn't work anymore. – Luke Dupin Oct 27 '18 at 19:32
  • @Markus Note that the `#![macro_use]` statement is *INSIDE* the macro-module, not outside. The `#![...]` syntax corresponds to having attributes apply to their containing scopes, e.g. `#![feature(...)]` (obviously this wouldn't make sense if written as `#[feature(...)]`; it would semantically require that the compiler enable certain features on specific items in a crate, rather than the whole root crate). So, as @LukeDupin said, the module system is a mess, though maybe for a different reason than at first glance. – user Dec 06 '18 at 17:22
  • I do wish that this answer mentioned how the construction isn't exactly idiomatic (that aside, I like the answer). In spite of its (non)-idiomaticity, it's interesting because placing it next to the idiomatic form makes it painfully obvious that macros interact with the module system in a different way than the usual constructions. Or it at least gives off a strong smell (as just demonstrated by @Markus having a gripe with it). – user Dec 06 '18 at 17:38
  • This works because in main.rs the line `mod node;` comes before `mod toad;`. Be careful when using `rustfmt`, as it may re-order these lines lexicographically. For me the fix was to put an empty newline between those lines. – Sebastian Sep 19 '19 at 18:28
  • "Macros can only be used after they have been defined." Thanks! – Lucius Hu Dec 13 '19 at 15:34
  • I prefer putting the annotation over the `mod` declaration since it prevents rustfmt from changing the order. – cambunctious Jul 10 '20 at 20:44
10

I have came across the same problem in Rust 1.44.1, and this solution works for later versions (known working for Rust 1.7).

Say you have a new project as:

src/
    main.rs
    memory.rs
    chunk.rs

In main.rs, you need to annotate that you are importing macros from the source, otherwise, it will not do for you.

#[macro_use]
mod memory;
mod chunk;

fn main() {
    println!("Hello, world!");
}

So in memory.rs you can define the macros, and you don't need annotations:

macro_rules! grow_capacity {
    ( $x:expr ) => {
        {
            if $x < 8 { 8 } else { $x * 2 }
        }
    };
}

Finally you can use it in chunk.rs, and you don't need to include the macro here, because it's done in main.rs:

grow_capacity!(8);

The upvoted answer caused confusion for me, with this doc by example, it would be helpful too.


Note: This solution does work, but do note as @ineiti highlighted in the comments, the order u declare the mods in the main.rs/lib.rs matters, all mods declared after the macros mod declaration try to invoke the macro will fail.

lnshi
  • 2,310
  • 3
  • 19
  • 42
knh190
  • 2,744
  • 1
  • 16
  • 30
  • 4
    @Shepmaster the upvoted answer has definition of macros and the import statement at the same place, so it caused confusion (for me). I was using `#[macro_use]` in definition. Compiler doesn't say it is misplaced. – knh190 Aug 03 '20 at 18:06
  • 1
    Thank you for this answer! I was confused by the accepted answer as well and could not figure it out till I read your explanation. – Prgrm.celeritas Oct 20 '20 at 16:55
  • @Shepmaster There is no mention of how macros work in the section you link to. Did you mean to link to some other part of the book? – detly Nov 06 '20 at 04:24
  • 1
    @detly no, because what my comment is pointing out is broader than macros. This answerer seemed confused that `mod { ... }` and `mod some_file` are the same thing and both create a module. The accepted answer already shows the usage of `#[macro_use]`, so _this_ answer doesn't really provide anything new. – Shepmaster Nov 06 '20 at 13:59
  • 2
    Also be sure to have the correct order of `mod` in `main.rs`. If you have `mod chunk; mod memory;`, then the macro call in `memory.rs` will fail. – ineiti Dec 12 '20 at 13:41
  • I think this is the clearest answer as well! The accepted answer wasn't enough for me to see that I had to use `pub (crate)` along with `#[macro_use]`, and it's worth noting that you never need to include the macro in other files. – Sasha Kondrashov Sep 07 '22 at 00:05