Print complete enum

Hi,

I would like to print all variants of an enum. So e.g. for

enum Foo {
    Abc,
    Xyz,
}

I would like to see something like "Abc/Xyz".

I implemented Display for Foo like this ...

impl Display for Foo {
     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
         write!(f, "{:?}/{:?}", Foo::Abc, Foo::Xyz)
     }
}

and then could do it with println!("{}", Foo::Abc) but using a variant to print the whole enum feels wrong.

Then I tried to use Action::iter() from the strum crate but could not figure out how to use it properly.

Any idea how this could be done?

Thanks.

Display is for printing values, you just want info about the type itself, you can just implement an associated function on the type:

impl Foo {
    fn print_variant_names() {
        todo!()
    }
}
fn main() {
    Foo::print_variant_names();
}

Ah! Right. I forgot that I can write general methods for enums.

Thanks. :slight_smile: