-
-
Save csknk/4ba26c0f0832d1c723c2b4640f9f315b 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() { | |
// 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
See https://static.rust-lang.org/doc/master/book/if-let.html