I'm having trouble understanding what exactly a tt
is in Rust's macro_rules!
macros.
From this answer, I thought that
tt
will match any single token or any pair of parenthesis/brackets/braces with their content.
However, the following example does not seem to follow this rule:
macro_rules! foo {
(content: $content:tt) => {
mod foo {
$content
}
}
}
foo! (
content: {
pub fn main() {
println!("Hello");
}
}
);
I would expect that the tt
would match everything contained in the {}
after content:
and that the result of the macro invocation would be
mod foo {
pub fn main() {
println!("Hello");
}
}
Instead, I get the following error message:
error: expected item, found `{`
--> src/main.rs:10:12
|
10 | content: {
| ^ expected item
What's going wrong here? Also, why does Rust tell me it's expecting an item
when I've told it to expect a tt
?