A macro matcher can include literal tokens, which lets a call read like a tiny domain-specific syntax.

Program

Play the program to choose the retry count in a macro call with a named argument.

retries
macro_named_argument.rs
Replay: real traced execution (multi-file project)
macro_rules! command_line {
    ($name:expr, retries = $count:expr) => {
        format!("{} retries={}", $name, $count)
    };
}

fn main() {
    let retries = 2;
    let command = command_line!("deploy", retries = retries);
    println!("{command}");
}
macro_rules! command_line {
    ($name:expr, retries = $count:expr) => {
        format!("{} retries={}", $name, $count)
    };
}

fn main() {
    let retries = 0;
    let command = command_line!("deploy", retries = retries);
    println!("{command}");
}
macro_rules! command_line {
    ($name:expr, retries = $count:expr) => {
        format!("{} retries={}", $name, $count)
    };
}

fn main() {
    let retries = 4;
    let command = command_line!("deploy", retries = retries);
    println!("{command}");
}
  1. retries ← 2, command ← "deploy retries=2"

    7fn main() {8    let retrie→ 2s = 2; //@retries=2, 0, 49    let comman→ "deploy retries=2"d = command_line!("deploy", retries = retries);10    println!("{command}");11}
    outputdeploy retries=2
  1. retries ← 0, command ← "deploy retries=0"

    7fn main() {8    let retrie→ 0s = 0;9    let comman→ "deploy retries=0"d = command_line!("deploy", retries = retries);10    println!("{command}");11}
    outputdeploy retries=0
  1. retries ← 4, command ← "deploy retries=4"

    7fn main() {8    let retrie→ 4s = 4;9    let comman→ "deploy retries=4"d = command_line!("deploy", retries = retries);10    println!("{command}");11}
    outputdeploy retries=4
literal tokens The matcher requires the literal `retries =` tokens at the call site.
fragment specifier `$count:expr` captures the expression after the named token.
DSL shape Literal tokens can make macro calls read like a small, checked syntax.