Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct Person {
  3. mood: Mood,
  4. }
  5.  
  6. #[derive(Debug)]
  7. enum Mood {
  8. Happy { duration: std::time::Duration },
  9. Sad { seriousness: u8, reason: String },
  10. }
  11.  
  12. impl Person {
  13. fn feed_cake(&mut self, how_much_cake: u8) {
  14. self.mood = match self.mood {
  15. mood @ Mood::Happy { .. } => {
  16. eprintln!("I can't get any happier.");
  17. mood
  18. }
  19. Mood::Sad {
  20. seriousness,
  21. reason,
  22. } => {
  23. if how_much_cake > seriousness {
  24. Mood::Happy {
  25. duration: std::time::Duration::from_secs(90),
  26. }
  27. } else {
  28. Mood::Sad {
  29. seriousness: seriousness - how_much_cake,
  30. reason: reason,
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }
  37.  
  38. fn main() {
  39. let mut dude = Person {
  40. mood: Mood::Sad {
  41. seriousness: 6,
  42. reason: "ran out of cake".to_owned(),
  43. },
  44. };
  45. eprintln!("{:?}", dude);
  46. dude.feed_cake(4);
  47. eprintln!("{:?}", dude);
  48. dude.feed_cake(4);
  49. eprintln!("{:?}", dude);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement