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
Copy file name to clipboardExpand all lines: src/expressions/match-expr.md
+16-9Lines changed: 16 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -20,8 +20,14 @@ MatchArm ->
20
20
OuterAttribute* Pattern MatchArmGuard?
21
21
22
22
MatchArmGuard ->
23
-
`if` Expression
24
-
| `if` Expression `&&` LetChain
23
+
`if` MatchConditions
24
+
25
+
MatchConditions ->
26
+
MatchCondition ( `&&` MatchCondition )*
27
+
28
+
MatchCondition ->
29
+
OuterAttribute* `let` Pattern `=` Scrutinee
30
+
| Expression
25
31
```
26
32
<!-- TODO: The exception above isn't accurate, see https://github.com/rust-lang/reference/issues/569 -->
27
33
@@ -165,7 +171,7 @@ match expression {
165
171
...
166
172
}
167
173
```
168
-
Here, `guard_expr` is evaluated and matched against `subpattern`. If the `if let` expression in the guard matches successfully and the arm’s body is executed. Otherwise, pattern matching continues to the next arm.
174
+
Here, `guard_expr` is evaluated and matched against `subpattern`. If the `if let` expression in the guard matches successfully, the arm's body is executed. Otherwise, pattern matching continues to the next arm.
169
175
170
176
r[expr.match.if.let.guard.behavior]
171
177
When the pattern matches successfully, the `if let` expression in the guard is evaluated:
@@ -209,7 +215,7 @@ match opt {
209
215
r[expr.match.if.let.guard.borrowing]
210
216
Before a guard (including an `if let` guard) is evaluated:
211
217
1. Pattern bindings are performed first
212
-
Variables from the outer match pattern (e.g., `x` in `Some(x)`) are bound and initialized. These bindings may involve moving, copying, or borrowing values from the scrutinee.
218
+
Variables from the outer match pattern (e.g., `x` in `Some(x)`) are bound and initialized. These bindings may involve moving, copying, or borrowing values from the scrutinee.
213
219
```rust,ignore
214
220
match Some(String::from("hello")) {
215
221
Some(s) if /* guard */ => { /* s is moved here */ }
@@ -221,12 +227,13 @@ Before a guard (including an `if let` guard) is evaluated:
221
227
* Moves from the scrutinee in the guard are subject to standard Rust ownership rules. Avoid moving what's needed later; already moved parts from the main pattern can be used.
222
228
* Variables successfully bound within the if let guard are in scope within the corresponding match arm body. Their scope extends to the arm after the guard condition is met.
223
229
```rust,ignore
224
-
let val = Some(vec![1, 2, 3]);
230
+
fn take<T>(value: T) -> Option<T> { Some(value) }
225
231
226
-
let result = match val {
227
-
Some(v) if let Some(_) = take(v) => "ok", // ERROR: cannot move out of `v`
228
-
_ => "nope",
229
-
};
232
+
let val = Some(vec![1, 2, 3]);
233
+
let result = match val {
234
+
Some(v) if let Some(_) = take(v) => "ok", // ERROR: cannot move out of `v`
235
+
_ => "nope",
236
+
};
230
237
```
231
238
In the above example, `v` is already bound in the outer pattern, and the guard attempts to move it --- this is not allowed. You can fix it by cloning or borrowing:
0 commit comments