Skip to content

Commit 66d2ead

Browse files
committed
Add more examples for | patterns in let in RFC 2175
1 parent ffecb55 commit 66d2ead

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

text/2175-if-while-or-patterns.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ in the `for` loop and the desugaring as per the section on grammar.
375375

376376
## Desugaring `let` statements with `|` in the top-level pattern
377377

378+
There continues to be an exhaustivity check in `let` statements,
379+
however this check will now be able to support multiple patterns.
380+
378381
This is a possible desugaring that a Rust compiler may do.
379382
While such a compiler may elect to implement this differently,
380383
these semantics should be kept.
@@ -404,6 +407,48 @@ Result
404407
}
405408
```
406409

410+
For example, the following code:
411+
412+
```rust
413+
{
414+
foo();
415+
bar();
416+
let Ok(index) | Err(index) = slice.binary_search(&thing);
417+
println!("{}", index);
418+
do_something_to(index)
419+
}
420+
```
421+
422+
can be desugared to
423+
424+
```rust
425+
{
426+
foo();
427+
bar();
428+
match slice.binary_search(&thing) {
429+
Ok(index) | Err(index) => {
430+
println!("{}", index);
431+
do_something_to(index)
432+
}
433+
}
434+
}
435+
```
436+
437+
It can also be desugared to:
438+
439+
```rust
440+
{
441+
foo();
442+
bar();
443+
let index = match slice.binary_search(&thing) {
444+
Ok(index) | Err(index) => index,
445+
}
446+
println!("{}", index);
447+
do_something_to(index)
448+
}
449+
```
450+
451+
(Both are equivalent)
407452
# Drawbacks
408453
[drawbacks]: #drawbacks
409454

0 commit comments

Comments
 (0)