enums

An enum is destructured similarly:

1. // `allow` required to silence warnings because only
2. // one variant is used.
3. #[allow(dead_code)]
4. enum Color {
5. // These 3 are specified solely by their name.
6. Red,
7. Blue,
8. Green,
9. // These likewise tie `u32` tuples to different names: color models.
10. RGB(u32, u32, u32),
11. HSV(u32, u32, u32),
12. HSL(u32, u32, u32),
13. CMY(u32, u32, u32),
14. CMYK(u32, u32, u32, u32),
15. }

17. fn main() {
18. let color = Color::RGB(122, 17, 40);
19. // TODO ^ Try different variants for `color`

21. println!("What color is it?");
22. // An `enum` can be destructured using a `match`.
23. match color {
24. Color::Red   => println!("The color is Red!"),
25. Color::Blue  => println!("The color is Blue!"),
26. Color::Green => println!("The color is Green!"),
27. Color::RGB(r, g, b) =>
28. println!("Red: {}, green: {}, and blue: {}!", r, g, b),
29. Color::HSV(h, s, v) =>
30. println!("Hue: {}, saturation: {}, value: {}!", h, s, v),
31. Color::HSL(h, s, l) =>
32. println!("Hue: {}, saturation: {}, lightness: {}!", h, s, l),
33. Color::CMY(c, m, y) =>
34. println!("Cyan: {}, magenta: {}, yellow: {}!", c, m, y),
35. Color::CMYK(c, m, y, k) =>
36. println!("Cyan: {}, magenta: {}, yellow: {}, key (black): {}!",
37. c, m, y, k),
38. // Don't need another arm because all variants have been examined
39. }
40. }

See also:

#[allow(...)], color models and enum