我想使用Rust宏在“定制”变量的旁边引入枚举变量。作为一个简单的例子:
macro_rules! make_beta {
() => {Beta}
}
enum Greek {
Alpha,
make_beta! ()
}
我的真正目标是要有一个家庭:
macro_rules! make_variants {
($($N:literal)+) => {
$(
Array$N([u8; $N]),
)+
}
}
enum Stuff {
Empty,
Something,
make_variants! { 1 2 3 4 5 6 7 8 }
}
which has Array1
through Array8
in addition to "bespoke" variants. Unfortunately neither of these compiles: it complains about the exclamation mark example.
如何使用宏介绍枚举变量?