Skip to content

Commit 3966e6b

Browse files
committed
fixed few reviews
1 parent e704b43 commit 3966e6b

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/expressions/match-expr.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ MatchExpression ->
99
MatchArms?
1010
`}`
1111
12-
Scrutinee -> Expression _except [StructExpression]_
12+
Scrutinee ->
13+
Expression _except_ [StructExpression]
1314
1415
MatchArms ->
1516
( MatchArm `=>` ( ExpressionWithoutBlock `,` | ExpressionWithBlock `,`? ) )*
1617
MatchArm `=>` Expression `,`?
1718
18-
MatchArm -> OuterAttribute* Pattern MatchArmGuard?
19+
MatchArm ->
20+
OuterAttribute* Pattern MatchArmGuard?
1921
20-
MatchArmGuard -> `if` Expression
21-
| `if` `let` Pattern Expression
22+
MatchArmGuard ->
23+
`if` Expression
24+
| `if` Expression `&&` LetChain
2225
```
2326
<!-- TODO: The exception above isn't accurate, see https://github.com/rust-lang/reference/issues/569 -->
2427

@@ -153,7 +156,7 @@ Moreover, by holding a shared reference while evaluating the guard, mutation ins
153156
154157
r[expr.match.if.let.guard]
155158
## If Let Guards
156-
Match arms can include `if let` guards to allow conditional pattern matching within the guard clause. This feature is currently unstable and requires the attribute. It is tracked in issue [#51114](https://github.com/rust-lang/rust/issues/51114).
159+
Match arms can include `if let` guards to allow conditional pattern matching within the guard clause.
157160
158161
r[expr.match.if.let.guard.syntax]
159162
```rust,ignore
@@ -162,11 +165,11 @@ match expression {
162165
...
163166
}
164167
```
165-
Here, `guard_expr` is evaluated and matched against `subpattern`. If the match succeeds, the guard evaluates to `true` and the arm is selected. Otherwise, pattern matching continues to the next arm.
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.
166169

167170
r[expr.match.if.let.guard.behavior]
168171
When the pattern matches successfully, the `if let` expression in the guard is evaluated:
169-
* If the inner pattern (`subpattern`) matches the result of `guard_expr`, the guard evaluates to `true`.
172+
* The guard proceeds if the inner pattern (`subpattern`) matches the result of `guard_expr`.
170173
* Otherwise, the next arm is tested.
171174

172175
```rust,ignore
@@ -176,7 +179,6 @@ let msg = match value {
176179
Some(x) if let Some(y) = Some(x - 1) => format!("Matched inner value: {}", y),
177180
_ => "No match".to_string(),
178181
};
179-
180182
```
181183

182184
r[expr.match.if.let.guard.scope]
@@ -225,14 +227,13 @@ Before a guard (including an `if let` guard) is evaluated:
225227
Some(v) if let Some(_) = take(v) => "ok", // ERROR: cannot move out of `v`
226228
_ => "nope",
227229
};
228-
229230
```
230231
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:
231232
```rust,ignore
232233
Some(v) if let Some(_) = take(v.clone()) => "ok",
233234
```
234235
> [!NOTE]
235-
> Unlike regular if guards, `if let` guards execute only once per match arm, even if the pattern uses the `|` operator to match multiple patterns. This avoids repeated evaluation and potential side effects.
236+
> Multiple matches using the `|` operator can cause the pattern guard and the side effects it has to execute multiple times. For example:
236237
> ```rust,ignore
237238
> use std::cell::Cell;
238239
>
@@ -241,7 +242,7 @@ Before a guard (including an `if let` guard) is evaluated:
241242
> 1 | _ if let Some(_) = { i.set(i.get() + 1); Some(1) } => {}
242243
> _ => {}
243244
> }
244-
> assert_eq!(i.get(), 1); // Guard not executed twice
245+
> assert_eq!(i.get(), 2); // Guard is executed twice
245246
> ```
246247
247248
r[expr.match.attributes]

0 commit comments

Comments
 (0)