Last active
June 3, 2022 11:43
-
-
Save kriogenia/eb4843c244b24e98cb2e065bda40d6ed to your computer and use it in GitHub Desktop.
if let
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 overflow(n: i32, max: i32) -> Option<i32> { | |
if n > max { | |
Some(n - max) | |
} else { | |
None | |
} | |
} | |
fn main() { | |
let n = 15; | |
// instead of this | |
match overflow(n, 10) { | |
Some(overflow) => println!("The input value has gone {overflow} over the maximum"), | |
_ => {} | |
} | |
// do this | |
if let Some(overflow) = overflow(n, 10) { | |
println!("The input value has gone {overflow} over the maximum") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment