Hmmm, I tried that too, but my naive approach didn't work at first (see Playground here).
I worked with macros previously which require a certain type to be in scope with a certain name, see radiorust::flt! or argmin::float! as two examples.
To give another example:
// We can define this macro outside `foo`:
macro_rules! works_on_type_T {
() => { println!("{:?}", ::std::mem::size_of::<T>()); };
}
fn foo<T>(v: i32) {
// But we must define this macro inside `foo`:
macro_rules! works_on_variable_v {
() => { println!("{:?}", ::std::mem::size_of_val(&v)); };
}
works_on_type_T!();
works_on_variable_v!();
}
fn main() {
foo::<u8>(12i32);
}
macro_rules! macros in Rust are partially hygienic, also called mixed hygiene. Specifically, they are hygienic when it comes to local variables, labels and $crate, but nothing else.
My understanding is that it is intentional, but that it isn't necessarily going to be how macros work in declarative macros 2.0 (aka macro aka decl_macro aka macros-by-example). But also a lot of things are still up in the air on that feature so take that with a grain of salt.