How do I use a macro across module files?

前端 未结 4 1945
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 13:56

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 m

4条回答
  •  独厮守ぢ
    2020-11-27 14:25

    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)
    

提交回复
热议问题