Skip to content

Instantly share code, notes, and snippets.

@kriogenia
Last active June 3, 2022 11:43
Show Gist options
  • Save kriogenia/eb4843c244b24e98cb2e065bda40d6ed to your computer and use it in GitHub Desktop.
Save kriogenia/eb4843c244b24e98cb2e065bda40d6ed to your computer and use it in GitHub Desktop.
if let
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