macro

Literal and attribute fragments

Literal captures literal syntax, while meta captures attribute contents. A macro can attach caller-selected attributes to generated items. Expected stdout (not executed): '8\n' Common mistake: A variable name does not match a literal fragment.

Formal Syntax & Structure

rust syntaxrust
$n:literal / $attr:meta

Working Example

Example Snippetrust
macro_rules! setting {    ($attr:meta, $n:literal) => { #[$attr] const LIMIT: i32 = $n; };}setting!(allow(dead_code), 8);fn main() { println!("{LIMIT}"); }
Rust Reference