Define a function using a macro
- We can use both tt and ident in the macro parameter definition.
- We can use the macro both inside and outside functions.
- The former will create functions scoped to the block where they were created. The latter will be global.
examples/macros/define-function/src/main.rs
macro_rules! define_with_tt { ($var:tt) => { fn $var() { println!("hi"); } } } macro_rules! define_with_ident { ($var:ident) => { fn $var() { println!("hi"); } } } fn main() { define_with_tt!(hi_tt); define_with_ident!(hi_ident); hi_tt(); hi_ident(); global_tt(); global_ident(); } define_with_tt!(global_tt); define_with_ident!(global_ident);