如何获取枚举的整数值? [英] How do I get the integer value of an enum?
本文介绍了如何获取枚举的整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以这样写结构:
enum Number {
One = 1,
Two = 2,
Three = 3,
Four = 4,
}
但目的是什么?我找不到任何方法来获取枚举变量的值.
but for what purpose? I can't find any method to get the value of an enum variant.
推荐答案
您可以通过将枚举变量转换为整数类型来获取值:
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);
}
这篇关于如何获取枚举的整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文