rust - Use macro across module files -
let's have 2 modules in separate files within same crate, crate has enabled macro_rules
. want utilize macros defined in 1 module in module.
// macros.rs #[macro_export] // or not? ineffectual this, afaik macro_rules! my_macro(...) // something.rs utilize macros; // utilize macros::my_macro; <-- unresolved import (for obvious reasons) my_macro!() // <-- how?
i nail compiler error "macro undefined: 'my_macro'
"... makes sense; macro scheme runs before module system. how work around that?
the accepted reply not right anymore, here updated 1 (rustc 1.1.0-stable
):
#[macro_use] mod foo { macro_rules! bar { () => () } } bar!(); // works
if want utilize macro in same crate, module macro defined in needs attribute #[macro_use]
.
if, however, want utilize macro in other crates well, macro needs attribute #[macro_export]
. if want utilize macros of other crates extern crate xxx;
statement needs attribute #[macro_use]
import macros crate or #[macro_use(cat, dog)]
utilize macros cat
, dog
.
note: macros can used after have been defined. means not work:
bar!(); #[macro_use] mod foo { macro_rules! bar { () => () } }
more info in this rust book article.
rust
No comments:
Post a Comment