macro_rules! create_function { ($func_name:ident) => { fn $func_name() { println!("You called {:?}()", stringify!($func_name)); } }; } create_function!(foo); create_function!(bar); fn main() { foo(); bar(); }
// notice how all the imports are revealed! #![feature(prelude_import)] #[prelude_import] use std::prelude::rust_2021::*; #[macro_use] extern crate std; // notice how the function bodies are shown! fn foo() { { ::std::io::_print(format_args!("You called {0:?}()\n", "foo")); }; } fn bar() { { ::std::io::_print(format_args!("You called {0:?}()\n", "bar")); }; } fn main() { foo(); bar(); }
โก #Rustlang Tip: cargo expand - Your X-ray Vision for Macros
cargo expand allows you to see the expanded code generated by macros, providing more insight into what's happening under the hood
Learn more here: docs.rs/crate/cargo-expand/0.1.3...
#RustMacros #Rust30by30 #Day13