Created
November 21, 2023 15:25
-
-
Save chrisbck/5b66068b1a459965dd54bfb662903aee to your computer and use it in GitHub Desktop.
Rust Matching Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Taken from YouTube video by Jeremy Chone: https://www.youtube.com/watch?v=VQdStWU2ewY | |
use Activity::*; | |
pub enum Activity{ | |
Sleeping(Option<u16>), | |
Skiing {resort: String}, | |
Coding | |
} | |
fn main() { | |
let activity = Sleeping(Some(9)); // Can be None | |
let is_weekend = false; | |
let is_powder_day = true; // good snow | |
let message = match (&is_powder_day, &is_weekend, &activity) { | |
(true, true, Sleeping(_) | Coding) => format!("Go Ski"), | |
(_, true, Sleeping(Some(h))) if h > &10 => format!("Wake up it's {} o'clock", h), | |
(_, false, Sleeping(Some(h))) if h > &8 => format!("Wake up it's {} o'clock", h), | |
(_, _, Sleeping(_)) => format!("ZZzzzz"), | |
(_, _, Skiing{resort}) => format!("Skiing {}", resort), | |
(_, _, Coding) => format!("Coding"), | |
}; | |
println!("{}", message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment