macro

Pattern fragments and edition behavior

In editions 2021 and 2024, pat accepts a top-level alternative; pat_param excludes that top-level form. Use pat_param when the macro itself supplies the separating vertical bar. Expected stdout (not executed): 'true true\n' Common mistake: Using pat before a literal vertical bar is not allowed in modern editions.

Formal Syntax & Structure

rust syntaxrust
$p:pat / $p:pat_param

Working Example

Example Snippetrust
macro_rules! either { ($v:expr, $left:pat_param | $right:pat_param) => { matches!($v, $left | $right) }; }macro_rules! test { ($v:expr, $p:pat) => { matches!($v, $p) }; }fn main() { println!("{} {}", either!(3, 1 | 3), test!(2, 2 | 4)); }
Rust Reference