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
An `if let` expression is semantically similar to an `if` expression but in place of a condition operand it expects the keyword `let` followed by a pattern, an `=` and a [scrutinee] operand.
@@ -115,28 +115,6 @@ let a = if let Some(1) = x {
115
115
assert_eq!(a, 3);
116
116
```
117
117
118
-
r[expr.if.let.desugaring]
119
-
An `if let` expression is equivalent to a [`match` expression] as follows:
120
-
121
-
<!-- ignore: expansion example -->
122
-
```rust,ignore
123
-
if let PATS = EXPR {
124
-
/* body */
125
-
} else {
126
-
/*else */
127
-
}
128
-
```
129
-
130
-
is equivalent to
131
-
132
-
<!-- ignore: expansion example -->
133
-
```rust,ignore
134
-
match EXPR {
135
-
PATS => { /* body */ },
136
-
_ => { /* else */ }, // () if there is no else
137
-
}
138
-
```
139
-
140
118
r[expr.if.let.or-pattern]
141
119
Multiple patterns may be specified with the `|` operator. This has the same semantics as with `|` in `match` expressions:
142
120
@@ -154,30 +132,73 @@ if let E::X(n) | E::Y(n) = v {
154
132
155
133
r[expr.if.let.lazy-bool]
156
134
The expression cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_].
157
-
Use of a lazy boolean operator is ambiguous with a planned feature change of the language (the implementation of if-let chains - see [eRFC 2947][_eRFCIfLetChain_]).
158
-
When lazy boolean operator expression is desired, this can be achieved by using parenthesis as below:
135
+
Scrutinee expressions also cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_] due to ambiguity and precedence with [chains of expressions][_ChainsOfExpressions_].
159
136
160
-
<!-- ignore: pseudo code -->
161
-
```rust,ignore
162
-
// Before...
163
-
if let PAT = EXPR && EXPR { .. }
137
+
r[expr.if.chains]
138
+
## Chains of expressions
164
139
165
-
// After...
166
-
if let PAT = ( EXPR && EXPR ) { .. }
140
+
The following is an example of chaining multiple expressions, mixing `let` bindings and boolean expressions, and with expressions able to reference pattern bindings from previous expressions:
A `while let` loop is semantically similar to a `while` loop but in place of a condition expression it expects the keyword `let` followed by a pattern, an `=`, a [scrutinee] expression and a block expression.
@@ -147,6 +145,29 @@ while let Some(v @ 1) | Some(v @ 2) = vals.pop() {
147
145
r[expr.loop.while.let.lazy-bool]
148
146
As is the case in [`if let` expressions], the scrutinee cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_].
149
147
148
+
r[expr.loop.while.chains]
149
+
### Chains of expressions
150
+
151
+
r[expr.loop.while.chains.intro]
152
+
The following is an example of chaining multiple expressions, mixing `let` bindings and boolean expressions, and with expressions able to reference pattern bindings from previous expressions:
153
+
154
+
```rust
155
+
fnmain() {
156
+
letouter_opt=Some(Some(1i32));
157
+
158
+
whileletSome(inner_opt) =outer_opt
159
+
&&letSome(number) =inner_opt
160
+
&&number==1
161
+
{
162
+
println!("Peek a boo");
163
+
break;
164
+
}
165
+
}
166
+
```
167
+
168
+
r[expr.loop.while.chains.limits]
169
+
The only remark is the fact that parenthesis (`while (let Some(a) = opt && (let Some(b) = a)) && b == 1`) and `||` operators (`while let A(x) = e1 || let B(x) = e2`) are not currently supported.
0 commit comments