Skip to content

Commit 85ed600

Browse files
committed
fixed if let guards documentation
1 parent a1cf397 commit 85ed600

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

src/expressions/match-expr.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ r[expr.match.if.let.guard.scope]
186186
* New variables bound inside the `if let` guard (e.g., `y` in the example above) are available within the body of the match arm where the guard evaluates to `true`, but are not accessible in other arms or outside the match expression.
187187

188188
```rust
189-
#![feature(if_let_guard)]
190-
191189
let opt = Some(42);
192190

193191
match opt {
@@ -223,26 +221,23 @@ Before a guard (including an `if let` guard) is evaluated:
223221
* You cannot move from the scrutinee inside the guard.
224222
* New bindings created inside the guard (e.g., via `if let Some(y) = expr`) are local to the guard and do not persist into the match arm body.
225223
```rust
226-
#![feature(if_let_guard)]
227-
228224
let val = Some(vec![1, 2, 3]);
229-
225+
230226
let result = match val {
231227
Some(v) if let Some(_) = take(v) => "ok", // ERROR: cannot move out of `v`
232228
_ => "nope",
233229
};
234-
230+
235231
```
236-
In the above example, `v` is already bound in the outer pattern, and the guard attempts to move itthis is not allowed. You can fix it by cloning or borrowing:
232+
In the above example, `v` is already bound in the outer pattern, and the guard attempts to move itthis is not allowed. You can fix it by cloning or borrowing:
237233
```rust
238234
Some(v) if let Some(_) = take(v.clone()) => "ok",
239235
```
240236
> [!NOTE]
241237
> 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.
242238
> ```rust
243-
> #![feature(if_let_guard)]
244239
> use std::cell::Cell;
245-
>
240+
>
246241
> let i: Cell<i32> = Cell::new(0);
247242
> match 1 {
248243
> 1 | _ if let Some(_) = { i.set(i.get() + 1); Some(1) } => {}

0 commit comments

Comments
 (0)