Skip to content

Commit 486818c

Browse files
committed
fixed match guard syntax
1 parent 303dab2 commit 486818c

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/expressions/match-expr.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ MatchArm ->
2020
OuterAttribute* Pattern MatchArmGuard?
2121
2222
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
2531
```
2632
<!-- TODO: The exception above isn't accurate, see https://github.com/rust-lang/reference/issues/569 -->
2733

@@ -165,7 +171,7 @@ match expression {
165171
...
166172
}
167173
```
168-
Here, `guard_expr` is evaluated and matched against `subpattern`. If the `if let` expression in the guard matches successfully and the arms 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.
169175

170176
r[expr.match.if.let.guard.behavior]
171177
When the pattern matches successfully, the `if let` expression in the guard is evaluated:
@@ -209,7 +215,7 @@ match opt {
209215
r[expr.match.if.let.guard.borrowing]
210216
Before a guard (including an `if let` guard) is evaluated:
211217
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.
213219
```rust,ignore
214220
match Some(String::from("hello")) {
215221
Some(s) if /* guard */ => { /* s is moved here */ }
@@ -221,12 +227,13 @@ Before a guard (including an `if let` guard) is evaluated:
221227
* 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.
222228
* 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.
223229
```rust,ignore
224-
let val = Some(vec![1, 2, 3]);
230+
fn take<T>(value: T) -> Option<T> { Some(value) }
225231
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+
};
230237
```
231238
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:
232239
```rust,ignore

0 commit comments

Comments
 (0)