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
This updates the `if` and `while` expressions so that they each are
presented as a single expression kind with multiple condition operators
instead of being logically separated from their `let` counterparts.
This also includes various fixes and additions.
An `if` expression is a conditional branch in program control.
22
-
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.
23
-
The condition operands must have the [boolean type].
24
-
If a condition operand evaluates to `true`, the consequent block is executed and any subsequent `else if` or `else` block is skipped.
25
-
If a condition operand evaluates to `false`, the consequent block is skipped and any subsequent `else if` condition is evaluated.
18
+
The syntax of an `if` expression is a sequence of one or more condition operands separated by `&&`,
19
+
followed by a consequent block, any number of `else if` conditions and blocks, and an optional trailing `else` block.
20
+
21
+
Condition operands must be either an [_Expression_] with a [boolean type] or a conditional `let` match.
22
+
If all of the condition operands evaluate to `true` and all of the `let` patterns successfully match their [scrutinee]s,
23
+
the consequent block is executed and any subsequent `else if` or `else` block is skipped.
24
+
If any condition operand evaluates to `false` or any `let` pattern does not match its scrutinee,
25
+
the consequent block is skipped and any subsequent `else if` condition is evaluated.
26
26
If all `if` and `else if` conditions evaluate to `false` then any `else` block is executed.
27
-
An if expression evaluates to the same value as the executed block, or `()` if no block is evaluated.
27
+
28
+
An `if` expression evaluates to the same value as the executed block, or `()` if no block is evaluated.
28
29
An `if` expression must have the same type in all situations.
29
30
30
31
```rust
@@ -37,6 +38,7 @@ if x == 4 {
37
38
println!("x is something else");
38
39
}
39
40
41
+
// `if` can be used as an expression.
40
42
lety=if12*15>150 {
41
43
"Bigger"
42
44
} else {
@@ -45,25 +47,23 @@ let y = if 12 * 15 > 150 {
45
47
assert_eq!(y, "Bigger");
46
48
```
47
49
48
-
## `if let`
50
+
## `if let` patterns
49
51
50
-
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
-
If the value of the scrutinee matches the pattern, the corresponding block will execute.
52
-
Otherwise, flow proceeds to the following `else` block if it exists.
53
-
Like `if` expressions, `if let` expressions have a value determined by the block that is evaluated.
52
+
`let` patterns in an `if` condition allow binding new variables into scope when the pattern matches successfully.
53
+
The following examples illustrate bindings using `let` patterns:
54
54
55
55
```rust
56
56
letdish= ("Ham", "Eggs");
57
57
58
-
//this body will be skipped because the pattern is refuted
58
+
//This body will be skipped because the pattern is refuted.
59
59
iflet ("Bacon", b) =dish {
60
60
println!("Bacon is served with {}", b);
61
61
} else {
62
62
// This block is evaluated instead.
63
63
println!("No bacon will be served");
64
64
}
65
65
66
-
//this body will execute
66
+
//This body will execute.
67
67
iflet ("Ham", b) =dish {
68
68
println!("Ham is served with {}", b);
69
69
}
@@ -73,23 +73,8 @@ if let _ = 5 {
73
73
}
74
74
```
75
75
76
-
`if` and `if let` expressions can be intermixed:
77
-
78
-
```rust
79
-
letx=Some(3);
80
-
leta=ifletSome(1) =x {
81
-
1
82
-
} elseifx==Some(2) {
83
-
2
84
-
} elseifletSome(y) =x {
85
-
y
86
-
} else {
87
-
-1
88
-
};
89
-
assert_eq!(a, 3);
90
-
```
91
-
92
-
Multiple patterns may be specified with the `|` operator. This has the same semantics as with `|` in `match` expressions:
76
+
Multiple patterns may be specified with the `|` operator.
77
+
This has the same semantics as with `|` in [`match` expressions]:
93
78
94
79
```rust
95
80
enumE {
@@ -103,10 +88,13 @@ if let E::X(n) | E::Y(n) = v {
103
88
}
104
89
```
105
90
106
-
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_].
91
+
## Chains of conditions
108
92
109
-
## Chains of expressions
93
+
Multiple condition operands can be separated with `&&`.
94
+
Similar to a `&&`[_LazyBooleanOperatorExpression_], each operand is evaluated from left-to-right until an operand evaluates as `false` or a `let` match fails,
95
+
in which case the subsequent operands are not evaluated.
96
+
97
+
The bindings of each pattern are put into scope to be available for the next condition operand and the consequent block.
110
98
111
99
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:
126
115
116
+
```rust
127
117
fnnested() {
128
118
letouter_opt=Some(Some(1i32));
129
119
@@ -137,39 +127,23 @@ fn nested() {
137
127
}
138
128
```
139
129
140
-
In other words, chains are equivalent to nested [`if let` expressions]:
130
+
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.
131
+
If a `||` expression is needed, then parentheses can be used. For example:
0 commit comments