Macro Rules
Macro Expression
Expand to a Value
macro_rules! can expand a call into an expression that is checked after expansion.
Program
Play the program to choose an id and build a label through a small expression macro.
macro_expr_label.rs
macro_rules! make_label {
($prefix:expr, $id:expr) => {
format!("{}-{}", $prefix, $id)
};
}
fn main() {
let id = ;
let label = make_label!("task", id);
println!("{label}");
}
macro_rules! make_label {
($prefix:expr, $id:expr) => {
format!("{}-{}", $prefix, $id)
};
}
fn main() {
let id = ;
let label = make_label!("task", id);
println!("{label}");
}
macro_rules! make_label {
($prefix:expr, $id:expr) => {
format!("{}-{}", $prefix, $id)
};
}
fn main() {
let id = ;
let label = make_label!("task", id);
println!("{label}");
}
macro_rules!
`macro_rules!` defines a declarative macro that rewrites syntax before type checking.
matcher
`$prefix:expr` and `$id:expr` capture expression fragments from the call site.
expansion
The macro expands to a `format!` expression that produces the final `String`.