keyword
Rust macro_rules: define a declarative macro
The contextual keyword macro_rules introduces a macro that matches Rust tokens and expands into code. Use it when reusable syntax is more useful than an ordinary function. Expected output (not executed): 14 Why: The macro expands into a multiplication with the argument grouped in parentheses. Common mistake: Macros are not ordinary functions; a macro can evaluate an inserted expression more than once if its expansion repeats it.
Formal Syntax & Structure
rust syntaxrust
macro_rules! name { ($x:expr) => { expression }; }Working Example
Example Snippetrust
macro_rules! double { ($value:expr) => { 2 * ($value) };}
fn main() { println!("{}", double!(3 + 4));}