How do I get the integer value of an enum?

后端 未结 1 2018
傲寒
傲寒 2020-12-10 14:38

It is possible to write constructions like this:

enum Number {
    One = 1,
    Two = 2,
    Three = 3,
    Four = 4,
}

but for what purpos

相关标签:
1条回答
  • 2020-12-10 15:18

    You get the value by casting the enum variant to an integral type:

    enum Thing {
        A = 1,
        B = 2,
    }
    
    fn main() {
        println!("{}", Thing::A as u8);
        println!("{}", Thing::B as u8);
    }
    
    0 讨论(0)
提交回复
热议问题