You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-let-else.md
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -225,7 +225,7 @@ Any refutable pattern that could be put into if-let's pattern position can be pu
225
225
If the pattern is irrefutable, rustc will emit the `irrefutable_let_patterns` warning lint, as it does with an irrefutable pattern in an `if let`.
226
226
227
227
The `else` block must _diverge_, meaning the `else` block must return the [never type (`!`)][never-type]).
228
-
This could be a keyword which diverges (returns `!`), or a panic.
228
+
This could be a keyword which diverges (returns `!`), such as `return`, `break`, `continue` or `loop { ... }`, a diverging function like `std::process::abort` or `std::process::exit`, or a panic.
229
229
230
230
If the pattern does not match, the expression is not consumed, and so any existing variables from the surrounding scope are
231
231
accessible as they would normally be.
@@ -315,6 +315,8 @@ While this feature can partly be covered by functions such `or_or`/`ok_or_else`
315
315
such functions do not exist automatically on custom enum types and require non-obvious and non-trivial implementation, and may not be map-able
316
316
to `Option`/`Result`-style functions at all (especially for enums where the "success" variant is contextual and there are many variants).
317
317
These functions will also not work for code which wishes to return something other than `Option` or `Result`.
318
+
Moreover, this does not cover diverging blocks that do something other than return with an error or target an enclosing `try` block,
319
+
for example if the diverging expression is `continue e` or `break 'outer_loop e`.
318
320
319
321
### Naming of `else` (`let ... otherwise { ... }`)
320
322
@@ -445,7 +447,7 @@ let Enum::Var1(x) = a || b || { return anyhow!("Bad x"); } && let Some(z) = x ||
445
447
// Complex. Both x and z are now in scope.
446
448
```
447
449
448
-
This is not a simple construct, and could be quite confusing to newcomers
450
+
This is not a simple construct, and could be quite confusing to newcomers.
449
451
450
452
That said, such a thing is not perfectly obvious to write today, and might be just as confusing to read:
0 commit comments