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` expression is a conditional branch in program control.
22
14
The syntax of an `if` expression is a condition operand, followed by a consequent block, any number of `else if` conditions and blocks, and an optional trailing `else` block.
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.
51
52
If the value of the scrutinee matches the pattern, the corresponding block will execute.
@@ -89,6 +90,27 @@ let a = if let Some(1) = x {
89
90
assert_eq!(a, 3);
90
91
```
91
92
93
+
An `if let` expression is equivalent to a [`match` expression] as follows:
94
+
95
+
<!-- ignore: expansion example -->
96
+
```rust,ignore
97
+
if let PATS = EXPR {
98
+
/* body */
99
+
} else {
100
+
/*else */
101
+
}
102
+
```
103
+
104
+
is equivalent to
105
+
106
+
<!-- ignore: expansion example -->
107
+
```rust,ignore
108
+
match EXPR {
109
+
PATS => { /* body */ },
110
+
_ => { /* else */ }, // () if there is no else
111
+
}
112
+
```
113
+
92
114
Multiple patterns may be specified with the `|` operator. This has the same semantics as with `|` in `match` expressions:
93
115
94
116
```rust
@@ -104,72 +126,30 @@ if let E::X(n) | E::Y(n) = v {
104
126
```
105
127
106
128
The expression cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_].
107
-
Scrutinee expressions also cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_] due to ambiguity and precedence with [chains of expressions][_ChainsOfExpressions_].
108
-
109
-
## Chains of expressions
110
-
111
-
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:
129
+
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_]).
130
+
When lazy boolean operator expression is desired, this can be achieved by using parenthesis as below:
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.
76
70
If the value of the scrutinee matches the pattern, the loop body block executes then control returns to the pattern matching statement.
@@ -123,26 +117,6 @@ while let Some(v @ 1) | Some(v @ 2) = vals.pop() {
123
117
124
118
As is the case in [`if let` expressions], the scrutinee cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_].
125
119
126
-
### Chains of expressions
127
-
128
-
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:
129
-
130
-
```rust
131
-
fnmain() {
132
-
letouter_opt=Some(Some(1i32));
133
-
134
-
whileletSome(inner_opt) =outer_opt
135
-
&&letSome(number) =inner_opt
136
-
&&number==1
137
-
{
138
-
println!("Peek a boo");
139
-
break;
140
-
}
141
-
}
142
-
```
143
-
144
-
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