In a Rust crate, a `macro_rules!` macro in `src/macros.rs` cannot be found from `src/report.rs`; which fixes work, and how do they differ?
answer
- textual scope ends with the module
- mod declaration order
- give the macro a path
- crate root and pub visibility
basics
~20 sTextual scope ends when the macros module closes. #[macro_use] on mod macros extends it to code declared after that line; pub(crate) use name; gives the macro an ordinary path to import; #[macro_export] moves it to the crate root and exports it to other crates.
solid answer
~40 sA `macro_rules!` macro's textual scope stops at the end of the module that defines it, so `report.rs` sees nothing and rustc reports ``cannot find macro `ensure_positive` in this scope``, with a hint about `#[macro_use]`. There are three standard fixes. `#[macro_use] mod macros;` extends the textual scope past the module, but still in source order: the `mod macros;` line must come before `mod report;`, or the error stays. `pub(crate) use ensure_positive;` written after the definition gives the macro **path-based scope**, so `report.rs` imports it with `use crate::macros::ensure_positive;` and module order stops mattering. `#[macro_export]` places the macro at the crate root with `pub` visibility, which also publishes it to every dependent crate. A plain `pub use` of a macro without `#[macro_export]` is `E0364`, because a macro's implicit visibility is `pub(crate)`.
code
rust · 24 lines// src/main.rs
mod report;
mod macros;
fn main() {
report::run();
}
// src/macros.rs
macro_rules! ensure_positive {
($v:expr) => {
assert!($v > 0, "{} must be positive", stringify!($v))
};
}
pub(crate) use ensure_positive;
// src/report.rs
use crate::macros::ensure_positive;
pub fn run() {
let n = 3;
ensure_positive!(n);
println!("ok {n}");
}go deeper
Recall that a macro defined in one module file is not automatically visible in sibling modules, and that #[macro_use] or a re-export fixes it.
Explain textual versus path-based scope across modules, why #[macro_use] depends on mod declaration order, and what pub(crate) use and #[macro_export] each change.
Pick the mechanism by audience: pub(crate) use for internal macros so files import them like functions, #[macro_export] only for macros that are deliberately public API.
Standardise one convention for internal macros across the workspace so module order is never load-bearing and exported macros stay a reviewed, intentional surface.
## Why the macro is not found A crate is a **module tree**: `main.rs` (or `lib.rs`) is the root, and each `mod name;` line attaches a child module whose source lives in `name.rs` or `name/mod.rs`. A `macro_rules!` macro by default has only **textual scope**: it is usable after its definition until the end of the enclosing module. A macro defined in `macros.rs` therefore exists only inside the `macros` module. `report.rs`, a sibling module, is outside that scope, and rustc 1.99.1 prints ``cannot find macro `ensure_positive` in this scope`` with the help line ``have you added the `#[macro_use]` on the module/import?``. There are three ways to widen that scope, and they behave differently. ## Fix 1: `#[macro_use]` on the module Putting `#[macro_use]` on `mod macros;` makes the macros defined inside that module stay in scope after the module ends. It is still **textual** scope, so it follows the order in the parent file: - `#[macro_use] mod macros;` followed by `mod report;` works: `report` is declared inside the extended scope. - `mod report;` followed by `#[macro_use] mod macros;` still fails with the same error, because `report` comes before the macros enter scope. That ordering dependency is why many crates keep `#[macro_use] mod macros;` as the first line of `lib.rs`, and why reordering module declarations can break the build. ## Fix 2: re-export with `pub(crate) use` Writing `pub(crate) use ensure_positive;` after the definition inside `macros.rs` gives the macro **path-based scope**: it becomes the item `crate::macros::ensure_positive`, and any module can import it with `use crate::macros::ensure_positive;` and call `ensure_positive!(n)`. Module order no longer matters; the example crate compiles with `mod report;` declared before `mod macros;`. Two details: 1. The `use` must sit inside the macro's textual scope, so after the definition. 2. The visibility can be at most `pub(crate)`. A macro's implicit visibility is `pub(crate)`, so `pub use ensure_positive;` is `E0364`, ``is only public within the crate, and cannot be re-exported outside``. ## Fix 3: `#[macro_export]` `#[macro_export]` changes the macro's implicit visibility to `pub` and places it at the **crate root** for path-based resolution, whatever module it was written in. Inside the crate it becomes `crate::ensure_positive!`; outside it is `mycrate::ensure_positive!`. That is the right tool for a macro that is part of the crate's public API and the wrong tool for an internal helper, because it publishes the macro to every dependent crate. ## Comparison | Mechanism | Kind of scope | Depends on `mod` order | Visible to other crates | |---|---|---|---| | Define before use, same module | Textual | Yes | No | | `#[macro_use] mod macros;` | Textual, extended | Yes | No | | `pub(crate) use name;` | Path-based | No | No | | `#[macro_export]` | Path-based, at the root | No | Yes | ## How lookup picks between them An unqualified call such as `ensure_positive!()` looks in textual scope first and only then in path-based scope, and a textual binding shadows a path-based one. A qualified call like `self::m!()` or `crate::macros::m!()` looks only at path-based scope. So a file that both imports `m` and defines its own `macro_rules! m` gets the local definition for `m!()` once that definition is in scope. ## Moving an old crate off `#[macro_use]` 1. After each definition in `macros.rs`, add `pub(crate) use name;`. 2. In each module that calls a macro, add `use crate::macros::name;`. 3. Remove `#[macro_use]` from the `mod macros;` line; the build then names every module still missing an import. 4. Leave exported macros alone: they keep `#[macro_export]` and their `$crate` paths. The matching legacy form for another crate's macros is `#[macro_use] extern crate name;`. Since Rust 1.30 a path such as `name::the_macro!()` or a `use name::the_macro;` import does the same job without it. ## Choosing - Internal macros used across a crate: `pub(crate) use` beside the definition, imported like functions. - Legacy layouts already built on `#[macro_use]`: keep the macros module first and say why in a comment. - Public macros: `#[macro_export]`, written with `$crate` paths so they work in other crates.
- Why does #[macro_use] mod macros; still fail when it is declared after mod report;?#[macro_use] extends the macro's textual scope beyond its module, but textual scope still follows source order in the parent: it covers what comes after the mod macros line, including modules declared later. A mod report; line above it is outside that range, so rustc still reports cannot find macro.
- A module defines macro_rules! m and also imports a different m with use; which one does an unqualified m!() call get?Once the local definition is in scope, that one: unqualified macro calls search textual scope first and fall back to path-based scope, and the Reference states that textual bindings shadow path-based ones. A call above the definition, or a qualified call such as self::m!(), resolves through path-based scope and reaches the imported macro.
- #[macro_use] on a module makes its macros visible everywhere in the crate, regardless of order.
- pub use on a macro_rules! macro exports it to other crates without #[macro_export].
- Every macro_rules! macro can be called by its module path, crate::macros::name!, by default.
- #[macro_export] keeps the macro at its module's path rather than the crate root.
- The order of mod declarations in main.rs can never change whether a crate compiles.