Skip to content

Commit 76e8bb5

Browse files
Fishrock123digama0
andauthored
let-else, Mario's edits
Excluding the block grammar clarifications for now, because those have evolved somewhat in Zulip. Co-authored-by: Mario Carneiro <[email protected]>
1 parent c0c2fcc commit 76e8bb5

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

text/0000-let-else.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ Any refutable pattern that could be put into if-let's pattern position can be pu
225225
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`.
226226

227227
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.
229229

230230
If the pattern does not match, the expression is not consumed, and so any existing variables from the surrounding scope are
231231
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`
315315
such functions do not exist automatically on custom enum types and require non-obvious and non-trivial implementation, and may not be map-able
316316
to `Option`/`Result`-style functions at all (especially for enums where the "success" variant is contextual and there are many variants).
317317
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`.
318320

319321
### Naming of `else` (`let ... otherwise { ... }`)
320322

@@ -445,7 +447,7 @@ let Enum::Var1(x) = a || b || { return anyhow!("Bad x"); } && let Some(z) = x ||
445447
// Complex. Both x and z are now in scope.
446448
```
447449

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.
449451

450452
That said, such a thing is not perfectly obvious to write today, and might be just as confusing to read:
451453
```rust

0 commit comments

Comments
 (0)