Print complete enum

PressRex profile image
by PressRex
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.

Source: View source

PressRex profile image
by PressRex

Subscribe to New Posts

Lorem ultrices malesuada sapien amet pulvinar quis. Feugiat etiam ullamcorper pharetra vitae nibh enim vel.

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

Read More