I would like to use a Rust macro to introduce enum variants alongside "bespoke" ones. As a simple illustration:
macro_rules! make_beta {
() => {Beta}
}
enum Greek {
Alpha,
make_beta! ()
}
My real goal is to have a family:
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.
How can I introduce enum variants with a macro?
!
being unexpected because it doesn't expect a macro invocation in that position. (See the Rust reference for more details.) – FrxstremArray$N
in a macro. – Sven Marnachsmallvec
crate may be useful. – Sven Marnach