Skip to content

Instantly share code, notes, and snippets.

@csknk
Forked from rust-play/playground.rs
Created May 7, 2020 13:57
Show Gist options
  • Save csknk/3d4ea8bbfa0c2690bdf8b0e8914fe8fd to your computer and use it in GitHub Desktop.
Save csknk/3d4ea8bbfa0c2690bdf8b0e8914fe8fd 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