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.
27
-
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.
20
+
The syntax of an `if` expression is a sequence of one or more condition operands separated by `&&`,
21
+
followed by a consequent block, any number of `else if` conditions and blocks, and an optional trailing `else` block.
28
22
29
23
r[expr.if.condition-bool]
30
-
The condition operands must have the [boolean type].
24
+
Condition operands must be either an [_Expression_] with a [boolean type] or a conditional `let` match.
31
25
32
26
r[expr.if.condition-true]
33
-
If a condition operand evaluates to `true`, the consequent block is executed and any subsequent `else if` or `else` block is skipped.
27
+
If all of the condition operands evaluate to `true` and all of the `let` patterns successfully match their [scrutinee]s,
28
+
the consequent block is executed and any subsequent `else if` or `else` block is skipped.
34
29
35
30
r[expr.if.else-if]
36
-
If a condition operand evaluates to `false`, the consequent block is skipped and any subsequent `else if` condition is evaluated.
31
+
If any condition operand evaluates to `false` or any `let` pattern does not match its scrutinee,
32
+
the consequent block is skipped and any subsequent `else if` condition is evaluated.
37
33
38
34
r[expr.if.else]
39
35
If all `if` and `else if` conditions evaluate to `false` then any `else` block is executed.
40
36
41
37
r[expr.if.result]
42
-
An if expression evaluates to the same value as the executed block, or `()` if no block is evaluated.
38
+
An `if` expression evaluates to the same value as the executed block, or `()` if no block is evaluated.
43
39
44
40
r[expr.if.type]
45
41
An `if` expression must have the same type in all situations.
@@ -54,6 +50,7 @@ if x == 4 {
54
50
println!("x is something else");
55
51
}
56
52
53
+
// `if` can be used as an expression.
57
54
lety=if12*15>150 {
58
55
"Bigger"
59
56
} else {
@@ -63,32 +60,25 @@ assert_eq!(y, "Bigger");
63
60
```
64
61
65
62
r[expr.if.let]
66
-
## `if let`
63
+
## `if let` patterns
67
64
68
65
r[expr.if.let.intro]
69
-
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.
70
-
71
-
r[expr.if.let.pattern]
72
-
If the value of the scrutinee matches the pattern, the corresponding block will execute.
66
+
`let` patterns in an `if` condition allow binding new variables into scope when the pattern matches successfully.
73
67
74
-
r[expr.if.let.else]
75
-
Otherwise, flow proceeds to the following `else` block if it exists.
76
-
77
-
r[expr.if.let.result]
78
-
Like `if` expressions, `if let` expressions have a value determined by the block that is evaluated.
68
+
The following examples illustrate bindings using `let` patterns:
79
69
80
70
```rust
81
71
letdish= ("Ham", "Eggs");
82
72
83
-
//this body will be skipped because the pattern is refuted
73
+
//This body will be skipped because the pattern is refuted.
84
74
iflet ("Bacon", b) =dish {
85
75
println!("Bacon is served with {}", b);
86
76
} else {
87
77
// This block is evaluated instead.
88
78
println!("No bacon will be served");
89
79
}
90
80
91
-
//this body will execute
81
+
//This body will execute.
92
82
iflet ("Ham", b) =dish {
93
83
println!("Ham is served with {}", b);
94
84
}
@@ -98,25 +88,9 @@ if let _ = 5 {
98
88
}
99
89
```
100
90
101
-
r[expr.if.let.else-if]
102
-
`if` and `if let` expressions can be intermixed:
103
-
104
-
```rust
105
-
letx=Some(3);
106
-
leta=ifletSome(1) =x {
107
-
1
108
-
} elseifx==Some(2) {
109
-
2
110
-
} elseifletSome(y) =x {
111
-
y
112
-
} else {
113
-
-1
114
-
};
115
-
assert_eq!(a, 3);
116
-
```
117
-
118
91
r[expr.if.let.or-pattern]
119
-
Multiple patterns may be specified with the `|` operator. This has the same semantics as with `|` in `match` expressions:
92
+
Multiple patterns may be specified with the `|` operator.
93
+
This has the same semantics as with `|` in [`match` expressions]:
120
94
121
95
```rust
122
96
enumE {
@@ -130,12 +104,14 @@ if let E::X(n) | E::Y(n) = v {
130
104
}
131
105
```
132
106
133
-
r[expr.if.let.lazy-bool]
134
-
The expression cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_].
135
-
Scrutinee expressions also cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_] due to ambiguity and precedence with [chains of expressions][_ChainsOfExpressions_].
136
-
137
107
r[expr.if.chains]
138
-
## Chains of expressions
108
+
## Chains of conditions
109
+
110
+
Multiple condition operands can be separated with `&&`.
111
+
Similar to a `&&`[_LazyBooleanOperatorExpression_], each operand is evaluated from left-to-right until an operand evaluates as `false` or a `let` match fails,
112
+
in which case the subsequent operands are not evaluated.
113
+
114
+
The bindings of each pattern are put into scope to be available for the next condition operand and the consequent block.
139
115
140
116
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:
The above is equivalent to the following without using chains of conditions:
155
132
133
+
```rust
156
134
fnnested() {
157
135
letouter_opt=Some(Some(1i32));
158
136
@@ -166,39 +144,22 @@ fn nested() {
166
144
}
167
145
```
168
146
169
-
In other words, chains are equivalent to nested [`if let` expressions]:
170
-
171
-
<!-- ignore: expansion example -->
172
-
```rust,ignore
173
-
if let PAT0 = EXPR0 && let PAT1 = EXPR1 {
174
-
/* body */
175
-
} else {
176
-
/* else */
177
-
}
178
-
```
147
+
If any condition operand is a `let` pattern, then none of the condition operands can be a `||`[lazy boolean operator expression][_LazyBooleanOperatorExpression_] due to ambiguity and precedence with the `let` scrutinee.
148
+
If a `||` expression is needed, then parentheses can be used. For example:
0 commit comments