How to Write Declarative Macros with macro_rules! in Rust

Define declarative macros in Rust using the macro_rules! construct to match code patterns and expand them at compile time.

Use the macro_rules! construct to define a declarative macro that matches input patterns and expands into code at compile time.

#[macro_export]
macro_rules! vec {
    ( $( $x:expr ),* ) => {
        {
            let mut temp_vec = Vec::new();
            $(
                temp_vec.push($x);
            )*
            temp_vec
        }
    };
}

The #[macro_export] attribute makes the macro available when the crate is imported. The pattern ( $( $x:expr ),* ) matches a comma-separated list of expressions, and the body generates code to push each expression into a new Vec.