Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 6, 2020 19:18
Show Gist options
  • Save rust-play/2afb6cb55e529067ff2946386f0698f5 to your computer and use it in GitHub Desktop.
Save rust-play/2afb6cb55e529067ff2946386f0698f5 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
// If x has a value, unwrap it and set y to it's value.
// Otherwise, set y to a default.
// ------------------------------------------------------------------------
// let x = Some(11); // Uncomment to test
let x = None;
let y;
if x.is_some() {
y = x.unwrap();
} else {
y = 42;
}
println!("y = {}", y);
// If a has a value, set b to this value.
// Otherwise, set b to a default.
// ------------------------------------------------------------------------
let a = Some(42);
// let a = None; // Uncomment to test
let b = match a {
Some(b) => b,
None => 13,
};
println!("b = {}", b);
// If arg has a value, set val to this value.
// Otherwise, set val to a default value of 42.
// -------------------------------------------------------------------------
let arg = Some(13);
// let arg = None; // Uncomment to test
let mut val = if let Some(val) = arg { val } else { 42 };
val += 1;
println!("val is {}", val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment