Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct Person {
- mood: Mood,
- }
- #[derive(Debug)]
- enum Mood {
- Happy { duration: std::time::Duration },
- Sad { seriousness: u8, reason: String },
- }
- impl Person {
- fn feed_cake(&mut self, how_much_cake: u8) {
- self.mood = match self.mood {
- mood @ Mood::Happy { .. } => {
- eprintln!("I can't get any happier.");
- mood
- }
- Mood::Sad {
- seriousness,
- reason,
- } => {
- if how_much_cake > seriousness {
- Mood::Happy {
- duration: std::time::Duration::from_secs(90),
- }
- } else {
- Mood::Sad {
- seriousness: seriousness - how_much_cake,
- reason: reason,
- }
- }
- }
- }
- }
- }
- fn main() {
- let mut dude = Person {
- mood: Mood::Sad {
- seriousness: 6,
- reason: "ran out of cake".to_owned(),
- },
- };
- eprintln!("{:?}", dude);
- dude.feed_cake(4);
- eprintln!("{:?}", dude);
- dude.feed_cake(4);
- eprintln!("{:?}", dude);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement