by adamch · 4y ago · 0 comments
It's still convenient, but in really limited circumstances, AFAICT. This week I'll be going through the various Rust projects I maintain at work, seeing if there's useful places for let-else.
If you want help from automation, you can wait a few days until the manual_let_else clippy lint arrives on nightly. It's going to be one of the pedantic lints, and recognizes some obvious places where let else would make sense (not all but many). It should arrive in one week-ish, depending on when the next clippy update is in nightly.
if foo.is_none() {
return;
}
let foo = foo.unwrap()
Now I can do simply let Some(foo_unwrapped) = foo else {
return;
}
which is prettier than the `if let (...)` to just unwrap it IMO. let foo_unwrapped = match foo {
Some(foo_unwrapped) => foo_unwrapped,
None => return,
};
Not as pretty, but you don't have to unwrap. let x_result = something;
let Ok(x) = x_result else {
...
}
But I expect that it will be used mostly with Option, as in "else continue" or "else break". if let Error(e) = my_func() {
//... Do something with e
}Edited to add: I don't mean to imply that you should always use `if let` for those cases - that may be but I reserve judgement on it.