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.
14
18
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.
19
+
20
+
r[expr.if.constraint]
15
21
The condition operands must have the [boolean type].
22
+
23
+
r[expr.if.condition-true]
16
24
If a condition operand evaluates to `true`, the consequent block is executed and any subsequent `else if` or `else` block is skipped.
25
+
26
+
r[expr.if.else-if]
17
27
If a condition operand evaluates to `false`, the consequent block is skipped and any subsequent `else if` condition is evaluated.
28
+
29
+
r[expr.if.else]
18
30
If all `if` and `else if` conditions evaluate to `false` then any `else` block is executed.
31
+
32
+
r[expr.if.result]
19
33
An if expression evaluates to the same value as the executed block, or `()` if no block is evaluated.
34
+
35
+
r[expr.if.type]
20
36
An `if` expression must have the same type in all situations.
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.
72
+
73
+
r[expr.if.let.pattern]
52
74
If the value of the scrutinee matches the pattern, the corresponding block will execute.
75
+
76
+
r[expr.if.let.else]
53
77
Otherwise, flow proceeds to the following `else` block if it exists.
78
+
79
+
r[expr.if.let.result]
54
80
Like `if` expressions, `if let` expressions have a value determined by the block that is evaluated.
55
81
56
82
```rust
@@ -74,6 +100,7 @@ if let _ = 5 {
74
100
}
75
101
```
76
102
103
+
r[expr.if.let.else-if]
77
104
`if` and `if let` expressions can be intermixed:
78
105
79
106
```rust
@@ -90,6 +117,7 @@ let a = if let Some(1) = x {
90
117
assert_eq!(a, 3);
91
118
```
92
119
120
+
r[expr.if.let.desugaring]
93
121
An `if let` expression is equivalent to a [`match` expression] as follows:
94
122
95
123
<!-- ignore: expansion example -->
@@ -111,6 +139,7 @@ match EXPR {
111
139
}
112
140
```
113
141
142
+
r[expr.if.let.or-pattern]
114
143
Multiple patterns may be specified with the `|` operator. This has the same semantics as with `|` in `match` expressions:
115
144
116
145
```rust
@@ -125,6 +154,7 @@ if let E::X(n) | E::Y(n) = v {
125
154
}
126
155
```
127
156
157
+
r[expr.if.let.restriction]
128
158
The expression cannot be a [lazy boolean operator expression][_LazyBooleanOperatorExpression_].
129
159
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
160
When lazy boolean operator expression is desired, this can be achieved by using parenthesis as below:
0 commit comments