vbezhenar
vbezhenar

Reputation: 12326

Use macro in different file

I'm building a library and I have the following structure:

src/lib.rs

mod a;
mod b;

src/b.rs:

macro_rules! x ...

src/a.rs:

x!()

this doesn't work. I tried to fiddle with #macro_export and #macro_use but without success. What's the correct approach to re-using macros in the same library but in different module and file?

Upvotes: 3

Views: 1109

Answers (1)

Vladimir Matveev
Vladimir Matveev

Reputation: 127771

In order for macros from submodules to be visible to "sibling" modules they have to be declared in a certain order, that is, the module with macros should be declared the first. It also should have #[macro_use] attribute attached. This should work:

#[macro_use] mod b;
mod a;

#[macro_use] attribute makes macros from the module visible to all code "below" the module declaration, that's why your original variant could not possibly work.

Upvotes: 9

Related Questions