macro

Block and statement fragments

A block supplies a grouped body; a statement fragment captures one statement. Keep an expression result when the caller needs it, and be deliberate about semicolons. Expected stdout (not executed): '12\n' Common mistake: Adding a semicolon to a block tail can change its result to unit.

Formal Syntax & Structure

rust syntaxrust
$body:block / $s:stmt

Working Example

Example Snippetrust
macro_rules! execute { ($body:block) => { $body }; }macro_rules! run_stmt { ($s:stmt) => {{ $s }}; }fn main() { let n = execute!({ let a = 4; a * 3 }); run_stmt!(println!("{n}")); }
Rust Reference