There's no smart and standard solution.
An obvious one is to declare the array yourself, by repeating the variants:
static VARIANTS: &[MyEnum] = &[
MyEnum::EnumVariant1,
MyEnum::EnumVariant2,
];
This is a reasonable solution. When your code evolve, it's frequent you discover you don't want all variants in your static array. Or that you need several arrays.
Alternatively, if there are many elements or several enums, you may create a macro for this purpose:
macro_rules! make_enum {
(
$name:ident $array:ident {
$( $variant:ident, )*
}
) => {
pub enum $name {
$( $variant, )*
}
static $array: &[$name] = &[
$( $name::$variant, )*
];
}
}
make_enum! (MyEnum VARIANTS {
EnumVariant1,
EnumVariant2,
});
This make_enum!
macro call would create both the enum and a static array called VARIANTS
.