Skip to content

Instantly share code, notes, and snippets.

@csknk
Forked from rust-play/playground.rs
Last active May 6, 2020 20:41
Show Gist options
  • Save csknk/6a5c95aa8a04ffafb8d9cc1b26adfc2d to your computer and use it in GitHub Desktop.
Save csknk/6a5c95aa8a04ffafb8d9cc1b26adfc2d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
// If `option` is Some<T>, unwrap it and stick the value in `x`.
if let Some(x) = option {
foo(x);
}
}
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