【发布时间】:2019-06-21 17:09:47
【问题描述】:
我已经看到宏中使用了@ 符号,但我在 Rust Book 或任何官方文档或博客文章中都找不到它的提及。例如,在this Stack Overflow answer 中是这样使用的:
macro_rules! instructions {
(enum $ename:ident {
$($vname:ident ( $($vty: ty),* )),*
}) => {
enum $ename {
$($vname ( $($vty),* )),*
}
impl $ename {
fn len(&self) -> usize {
match self {
$($ename::$vname(..) => instructions!(@count ($($vty),*))),*
}
}
}
};
(@count ()) => (0);
(@count ($a:ty)) => (1);
(@count ($a:ty, $b:ty)) => (2);
(@count ($a:ty, $b:ty, $c:ty)) => (3);
}
instructions! {
enum Instruction {
None(),
One(u8),
Two(u8, u8),
Three(u8, u8, u8)
}
}
fn main() {
println!("{}", Instruction::None().len());
println!("{}", Instruction::One(1).len());
println!("{}", Instruction::Two(1, 2).len());
println!("{}", Instruction::Three(1, 2, 3).len());
}
从用法看来,它似乎是用于声明另一个宏,它是主宏的本地宏。
这个符号是什么意思,为什么要使用它而不是创建另一个顶级宏?
【问题讨论】:
标签: syntax rust rust-macros rust-decl-macros