Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 7, 2020 13:56
Show Gist options
  • Save rust-play/1cc24a93f14b59fbc1e3426f88955109 to your computer and use it in GitHub Desktop.
Save rust-play/1cc24a93f14b59fbc1e3426f88955109 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
// Match is an expression as well as a statement. This example shows how
// a variable is set based on an Option enum.
// ------------------------------------------
let msg_option = Some("+++MELON MELON MELON+++"); // Some variant
let msg_option_empty = None; // None variant
let msg = match msg_option {
Some(m) => m, // m is the unwrapped data from the option
None => "Out of Cheese Error",
};
println!("msg = {}", msg);
// As above, but handling the None variant
let msg_2 = match msg_option_empty {
Some(m) => m, // In this case we won't enter this branch
None => "Out of Cheese Error", // msg_2 set to this value
};
println!("msg_2 = {}", msg_2);
}
// Output:
// msg = +++MELON MELON MELON+++
// msg_2 = Out of Cheese Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment