I have an enum with many values
enum Foo {
Bar = 0x00,
Baz = 0x01,
Qux = 0x02,
...
Quux = 0xFF
}
and sometimes I'd like to write the name of one of its values to a stream. I can derive Debug
and do
writer.write(format!("I am {:?}", Foo::Quux).as_bytes())
which will output e.g. I am Quux
. That's fine, except that
- I want to do this for user-facing output, so Debug isn't appropriate
- It would be very helpful to get the enum as a string (rather than writing directly to a stream), because then I can incorporate its length into some wonky formatting calculations I want to do.
What's the best way to achieve this?
Since the names of enum variants are fixed, you don't need to allocate a
String
, a&'static str
will suffice. A macro can remove the boilerplate:Even better, you can derive these with a crate like strum_macros.
In strum 0.10, you can use
AsStaticRef
/AsStaticStr
to do the exact same code:In strum 0.9, the string slice's lifetime is not
'static
in this case:Probably the easiest way would be to implement
Display
by calling intoDebug
:Then you can use
to_string()
to get aString
representation:If you have many enums which you want to print, you can write a trivial macro to generate the above implementation of
Display
for each of them.Unfortunately, in Rust reflective programming is somewhat difficult. There is no standard way, for example, to get a list of all variants of a C-like enum. Almost always you have to abstract the boilerplate with custom-written macros (or finding something on crates.io). Maybe this will change in future if someone would write an RFC and it would get accepted.