fork download
  1. use std::borrow::Cow;
  2. use std::fmt;
  3.  
  4. enum Color {
  5. Red,
  6. Green,
  7. Blue,
  8. Rgb { r: u8, g: u8, b: u8 },
  9. }
  10.  
  11. impl Color {
  12. fn to_str(&self) -> Cow<str> {
  13. use Color::*;
  14. match *self {
  15. Red => "#FF0000".into(),
  16. Green => "#00FF00".into(),
  17. Blue => "#0000FF".into(),
  18. Rgb { r, g, b } => format!("#{:02X}{:02X}{:02X}", r, g, b).into(),
  19. }
  20. }
  21. }
  22.  
  23. impl fmt::Display for Color {
  24. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  25. f.write_str(&self.to_str())
  26. }
  27. }
  28.  
  29. fn main() {
  30. let color = Color::Rgb {
  31. r: 255,
  32. g: 255,
  33. b: 255,
  34. };
  35. println!("{}", color);
  36. }
  37.  
Success #stdin #stdout 0s 4524KB
stdin
Standard input is empty
stdout
#FFFFFF