Answers for "rust enum to string"

0

rust enum to string

enum Foo {
    Bar = 0x00,
    Baz = 0x01,
    Qux = 0x02,
    // ...
    Quux = 0xFF,
}

impl fmt::Display for Foo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
        // or, alternatively:
        // fmt::Debug::fmt(self, f)
    }
}

let s: String = Foo::Quux.to_string();
Posted by: Guest on March-07-2022

Browse Popular Code Answers by Language