macro
Expression fragments in Rust 2024
The edition of the macro definition determines which expression forms match. The 2024 expr accepts top-level const blocks; expr_2021 preserves the older matching rule. The edition comparison fixture uses the same source in both editions. Expected stdout (not executed): 'expression\n' Common mistake: Assuming that switching editions cannot change which arm wins.
Formal Syntax & Structure
rust syntaxrust
$e:expr / $e:expr_2021Working Example
Example Snippetrust
macro_rules! classify { ($e:expr) => { "expression" }; (const $body:block) => { "const arm" };}fn main() { println!("{}", classify!(const { 3 })); }