In the following trivial example, LLVM fails to optimize the unwrap() away. While its somewhat contrived in the sense that a match or if let should be preferred, more complicated logic ends up with similar blocks eventually.
pub fn print_if_some(arg: Result<u64, u32>) {
if arg.is_ok() {
let v = arg.unwrap();
if v == 42 {
println!("{}", v);
}
return;
}
}
Godbolt link for the same is https://godbolt.org/z/xrT1GfG7e you can see the test and jne for panic'ing.
In the following trivial example, LLVM fails to optimize the
unwrap()away. While its somewhat contrived in the sense that amatchorif letshould be preferred, more complicated logic ends up with similar blocks eventually.Godbolt link for the same is https://godbolt.org/z/xrT1GfG7e you can see the
testandjnefor panic'ing.