Declarative Macros with macro_rules!

Updated

October 31, 2026

Declarative Macros with macro_rules!

Declarative macros match against token patterns and expand into Rust code at compile time:

macro_rules! make_vec {
    ( $( $x:expr ),* ) => {
        {
            let mut temp_vec = Vec::new();
            $( temp_vec.push($x); )*
            temp_vec
        }
    };
}

fn main() {
    let v = make_vec![1, 2, 3];
    println!("Macro created vec: {:?}", v);
}