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-if-while-or-patterns.md
+80-11
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,11 @@
6
6
# Summary
7
7
[summary]: #summary
8
8
9
-
Enables "or" patterns for [`if let`](https://github.com/rust-lang/rfcs/pull/160) and [`while let`](https://github.com/rust-lang/rfcs/pull/214) expressions. In other words, examples like the following are now possible:
Enables "or" patterns for [`if let`] and [`while let`] expressions. In other
13
+
words, examples like the following are now possible:
10
14
11
15
```rust
12
16
enumE<T> {
@@ -27,7 +31,12 @@ while let A(x) | B(x) = source() {
27
31
# Motivation
28
32
[motivation]: #motivation
29
33
30
-
While nothing in this RFC is currently impossible in Rust, the changes the RFC proposes improves the ergonomics of control flow when dealing with `enum`s (sum types) with three or more variants where the program should react in one way to a group of variants, and another way to another group of variants. Examples of when such sum types occur are protocols and when dealing with languages (ASTs).
34
+
While nothing in this RFC is currently impossible in Rust, the changes the RFC
35
+
proposes improves the ergonomics of control flow when dealing with `enum`s
36
+
(sum types) with three or more variants where the program should react in one
37
+
way to a group of variants, and another way to another group of variants.
38
+
Examples of when such sum types occur are protocols, when dealing with
39
+
languages (ASTs), and non-trivial iterators.
31
40
32
41
The following snippet (written with this RFC):
33
42
@@ -56,7 +65,51 @@ match expr {
56
65
}
57
66
```
58
67
59
-
With `while let`, the ergonomics and in particular the readability can be significantly improved.
[RFC 2005](https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md#examples), in describing the third example in the section "Examples", refers to patterns with `|` in them as "or" patterns. This RFC adopts the same terminology.
While the "sum" of all patterns in `match` must be irrefutable, or in other words: cover all cases, be exhaustive, this is not the case (currently) with `if/while let`, which may have a refutable pattern. This RFC does not change this.
140
+
[RFC 2005], in describing the third example in the section "Examples", refers to
141
+
patterns with `|` in them as "or" patterns. This RFC adopts the same terminology.
88
142
89
-
The RFC only extends the use of or-patterns from `match`es to `if let` and `while let` expressions.
143
+
While the "sum" of all patterns in `match` must be irrefutable, or in other
144
+
words: cover all cases, be exhaustive, this is not the case (currently) with
145
+
`if/while let`, which may have a refutable pattern.
146
+
This RFC does not change this.
147
+
148
+
The RFC only extends the use of or-patterns from `match`es to `if let` and
149
+
`while let` expressions.
90
150
91
151
For examples, see [motivation].
92
152
@@ -95,9 +155,12 @@ For examples, see [motivation].
The changes proposed in this RFC can be implemented by transforming the `if/while let` constructs with a syntax-lowering pass into `match` and `loop` + `match` expressions.
193
+
The changes proposed in this RFC can be implemented by transforming the
194
+
`if/while let` constructs with a syntax-lowering pass into `match` and
195
+
`loop` + `match` expressions.
131
196
132
197
### Examples, `if let`
133
198
134
-
These examples are extensions on the [`if let` RFC](https://github.com/rust-lang/rfcs/pull/160). Therefore, the RFC avoids
0 commit comments