Toy example:
macro_rules! boo {
($T:ident) => {
let x: $T;
};
}
fn main() {
boo!(i32); // WORKS
boo!(Option<i32>); // PROBLEM
}
boo!(Option<i32>);
causes the error:
error: no rules expected the token `<`
--> src/main.rs:9:16
|
9 | boo!(Option<i32>);
| ^
I can work around it with:
type Opti32 = Option<i32>;
boo!(Opti32);
But it is too boring to add an alias for every usage of macros.
Is it possible to use macros like boo!(Option<i32>);
and hide
the difficulty inside macro_rules
?
$T:ident
can only match an ident
ifier.
If you want $T
to match any type, even if it's not a single identifier, you should use $T:ty
instead:
macro_rules! boo {
($T:ty) => {
let x: $T;
}
}
ident
and ty
are called "fragment specifiers" because they specify what kind of code fragment the metavariable $T
can match. The first edition of the Rust book has a chapter on macros, including a list of possible fragment specifiers; you should definitely familiarize yourself with the contents of this chapter before trying to write a macro.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With