C-like

enum can also be used as C-like enums.

  1. // An attribute to hide warnings for unused code.
  2. #![allow(dead_code)]
  3. // enum with implicit discriminator (starts at 0)
  4. enum Number {
  5. Zero,
  6. One,
  7. Two,
  8. }
  9. // enum with explicit discriminator
  10. enum Color {
  11. Red = 0xff0000,
  12. Green = 0x00ff00,
  13. Blue = 0x0000ff,
  14. }
  15. fn main() {
  16. // `enums` can be cast as integers.
  17. println!("zero is {}", Number::Zero as i32);
  18. println!("one is {}", Number::One as i32);
  19. println!("roses are #{:06x}", Color::Red as i32);
  20. println!("violets are #{:06x}", Color::Blue as i32);
  21. }

See also:

casting