Description
fn main() {
let Some(x) = Some(1)
else if true {
return;
} else {
return;
}
}
results in the following error but should compile imo:
error: conditional `else if` is not supported for `let...else`
--> src/main.rs:3:10
|
3 | else if true {
| ^^ expected `{`
|
help: try placing this code inside a block
|
3 ~ else { if true {
4 | return;
5 | } else {
6 | return;
7 ~ } }
|
the improved diagnostic has been added in #89974 which refers to #87335 (comment) by @joshtriplett:
let ... else if
definitely isn't something the RFC specifies, and I don't think we should. A better error message might make sense, though ("cannot useelse if
withlet ... else
").
I personally disagree with "and I don't think we should". For me this not working was fairly surprising and conflicts with my intuition of let else
as a shorthand for
let Some(var) = expr else { return; }
let var = if let Some(var) = expr {
var
} else {
return
}
or as stated in the RFC
This is a counterpart to
if let
expressions, and the pattern matching works identically, except that the value from the pattern match is assigned to the surrounding scope rather than the block's scope.
I would like to add support for this myself or mentor someone adding this. I am not sure what process is required here. If not desired, please FCP close this.