-
-
Save rust-play/4d32a45ec9dd3903421d36bfca44949f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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