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
Match arms can accept _match guards_ to further refine the criteria for matching a case.
128
124
129
-
r[expr.match.guard.type]
130
-
Pattern guards appear after the pattern and consist of a `bool`-typed expression following the `if` keyword.
125
+
r[expr.match.guard.condition]
126
+
Pattern guards appear after the pattern following the `if` keyword and consist of an [Expression] with a [boolean type][type.bool] or a conditional `let` match.
131
127
132
128
r[expr.match.guard.behavior]
133
-
When the pattern matches successfully, the pattern guard expression is executed.
134
-
If the expression evaluates to true, the pattern is successfully matched against.
129
+
When the pattern matches successfully, the pattern guard is executed. If all of the guard condition operands evaluate to `true` and all of the `let` patterns successfully match their [scrutinee]s, the match arm is successfully matched against and the arm body is executed.
135
130
136
131
r[expr.match.guard.next]
137
132
Otherwise, the next pattern, including other matches with the `|` operator in the same arm, is tested.
@@ -168,68 +163,73 @@ Before evaluating the guard, a shared reference is taken to the part of the scru
Guardscanuse `let` patterns to conditionally match a scrutinee and to bind new variables into scope when the pattern matches successfully.
180
174
181
-
r[expr.match.if.let.guard.syntax]
182
-
```rust
183
-
enumCommand {
184
-
Run(String),
185
-
Stop,
186
-
}
175
+
> [!EXAMPLE]
176
+
> In this example, the guard condition `letSome(first_char) = name.chars().next()` is evaluated.If the `iflet` expression successfully matches (i.e., the string has at least one character), the arm's body is executed with both `name` and `first_char` available.Otherwise, pattern matching continues to the next arm.
177
+
>
178
+
> The key point is that the `iflet` guard creates a new binding (`first_char`) that's only available if the guard succeeds, and this binding can be used alongside the original pattern bindings (`name`) in the arm's body.
// Both `name` and `first_char` are available here
191
-
println!("Running: {} (starts with '{}')", name, first_char);
192
-
}
193
-
Command::Run(name) => {
194
-
println!("Cannot run command: {}", name);
195
-
}
196
-
_=> {}
197
-
}
198
-
```
199
-
Here, the guard condition `let Some(first_char) = name.chars().next()` is evaluated. If the `if let` expression successfully matches (i.e., the string has at least one character), the arm's body is executed with both `name` and `first_char` available. Otherwise, pattern matching continues to the next arm.
198
+
r[expr.match.guard.chains]
199
+
## Matchguardchains
200
200
201
-
The key point is that the `if let` guard creates a new binding (`first_char`) that's only available if the guard succeeds, and this binding can be used alongside the original pattern bindings (`name`) in the arm's body.
0 commit comments