C-like

enum can also be used as C-like enums.

1. // An attribute to hide warnings for unused code.
2. #![allow(dead_code)]

4. // enum with implicit discriminator (starts at 0)
5. enum Number {
6. Zero,
7. One,
8. Two,
9. }

11. // enum with explicit discriminator
12. enum Color {
13. Red = 0xff0000,
14. Green = 0x00ff00,
15. Blue = 0x0000ff,
16. }

18. fn main() {
19. // `enums` can be cast as integers.
20. println!("zero is {}", Number::Zero as i32);
21. println!("one is {}", Number::One as i32);

23. println!("roses are #{:06x}", Color::Red as i32);
24. println!("violets are #{:06x}", Color::Blue as i32);
25. }

See also:

casting