Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 6, 2020 20:38
Show Gist options
  • Save rust-play/4d32a45ec9dd3903421d36bfca44949f to your computer and use it in GitHub Desktop.
Save rust-play/4d32a45ec9dd3903421d36bfca44949f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
let arg: Option<u8> = Some(42);
//let arg: Option<u8> = None;
let a: u8;
// If the Option `arg` has Some() value, the value is bound to the
// identifier in the pattern - in this case, `x`. In other words, if `arg`
// has a value, `x` is bound to the result of unwrapping `arg`.
// If `arg` is None, this branch isn't entered.
if let Some(x) = arg {
// If `arg` is None, this branch isn't entered.
// `x` is the result of unwrapping `arg`.
println!("x = {}", x);
a = x;
} else {
// If `arg` is None, set a default value for `a`.
a = 13;
}
println!("{}", a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment