Skip to content

Instantly share code, notes, and snippets.

@chrisbck
Created November 21, 2023 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbck/5b66068b1a459965dd54bfb662903aee to your computer and use it in GitHub Desktop.
Save chrisbck/5b66068b1a459965dd54bfb662903aee to your computer and use it in GitHub Desktop.
Rust Matching Example
// 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