Macro Rules
Macro Matcher
Named Syntax
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.
macro_named_argument.rs
macro_rules! command_line {
($name:expr, retries = $count:expr) => {
format!("{} retries={}", $name, $count)
};
}
fn main() {
let retries = ;
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 = ;
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 = ;
let command = command_line!("deploy", retries = retries);
println!("{command}");
}
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.