Why can I not access a variable declared in a macro unless I pass in the name of the variable?

后端 未结 1 1157
-上瘾入骨i
-上瘾入骨i 2021-02-20 15:23

I have this macro:

macro_rules! set_vars {
    ( $($x:ident),* ) => {
        let outer = 42;
        $( let $x = outer; )*
    }
}                                    


        
相关标签:
1条回答
  • 2021-02-20 16:04

    Yes, this is macro hygiene. Identifiers declared within the macro are not available outside of the macro (and vice versa). Rust macros are not C macros (that is, Rust macros are more than glorified text replacement).

    See also:

    • The Little Book of Rust Macros
    • A Practical Intro to Macros
    • So, what are hygienic macros anyway?
    0 讨论(0)
提交回复
热议问题