Skip to content

Instantly share code, notes, and snippets.

@csknk
Forked from rust-play/playground.rs
Created May 6, 2020 19:18
Show Gist options
  • Save csknk/4ba26c0f0832d1c723c2b4640f9f315b to your computer and use it in GitHub Desktop.
Save csknk/4ba26c0f0832d1c723c2b4640f9f315b 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);
}
@csknk
Copy link
Author

csknk commented May 6, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment