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
* The `if let` guard may refer to variables bound by the outer match pattern.
185
185
* 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.
Before a guard (including an `if let` guard) is evaluated:
210
210
1. Pattern bindings are performed first
211
211
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.
212
-
```rust
212
+
```rust,ignore
213
213
match Some(String::from("hello")) {
214
214
Some(s) if /* guard */ => { /* s is moved here */ }
215
215
_ => {}
@@ -219,7 +219,7 @@ Before a guard (including an `if let` guard) is evaluated:
219
219
* It runs using a shared borrow of the scrutinee
220
220
* You cannot move from the scrutinee inside the guard.
221
221
* 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.
222
-
```rust
222
+
```rust,ignore
223
223
let val = Some(vec![1, 2, 3]);
224
224
225
225
let result = match val {
@@ -234,7 +234,7 @@ Before a guard (including an `if let` guard) is evaluated:
234
234
```
235
235
> [!NOTE]
236
236
> 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.
0 commit comments