Macro is visible out of its module, why?

I can see in my docs that rialight_util contains a root map macro, but that macro should be at rialight_util::template_string only. It's visible both at rialight_util and at rialight_util::template_string.

I'm exposing the template_string module without #[macro_use].

In the playground I've played with removing #[macro_export], but then the pub use macro_name; item doesn't work.

For historical reasons, the scoping of macros by example does not work entirely like items.

Make sure you first read these materials about basic scoping rules of declarative macros before writing and using macros:

So The Little Book of Rust Macros has this example:

mod a {
    // X!(); // undefined
mod b {
    // X!(); // undefined
    macro_rules! X { () => {}; }
    X!(); // defined
mod c {
    // X!(); // undefined

Fine, I understand it. But there's no way to not use #[macro_export] because I need rialight_util::template_string::map to be accessible from anywhere, not private to that module.

The book says #[macro_export] exports the macro to the crate root... this is the problem.

Here's the source:

Sounds like you want a macro to be put into a specified path.

The best answer would be using a macro2.0 :
Rust Playground

Otherwise, the only choice is to macro_export it in the root and reexport like what you do now.

1 Like

I believe you can also use pub use template_string; right below the definition of the macro to export it using the regular scoping rules.

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.