How can I create hygienic identifiers in code generated by procedural macros?
问题 When writing a declarative ( macro_rules! ) macro, we automatically get macro hygiene . In this example, I declare a variable named f in the macro and pass in an identifier f which becomes a local variable: macro_rules! decl_example { ($tname:ident, $mname:ident, ($($fstr:tt),*)) => { impl std::fmt::Display for $tname { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let Self { $mname } = self; write!(f, $($fstr),*) } } } } struct Foo { f: String, } decl_example!(Foo, f,