Skip to content

Commit 16d25db

Browse files
authored
Clarify Matching Values section (#2833)
1 parent a0edd51 commit 16d25db

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/pattern-matching/match.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@ Key Points:
4141

4242
- Match guards as a separate syntax feature are important and necessary when we
4343
wish to concisely express more complex ideas than patterns alone would allow.
44-
- They are not the same as separate `if` expression inside of the match arm. An
45-
`if` expression inside of the branch block (after `=>`) happens after the
46-
match arm is selected. Failing the `if` condition inside of that block will
47-
result in other arms of the original `match` expression being considered.
44+
- Match guards are different from `if` expressions after the `=>`. An `if`
45+
expression is evaluated after the match arm is selected. Failing the `if`
46+
condition inside of that block won't result in other arms of the original
47+
`match` expression being considered. In the following example, the wildcard
48+
pattern `_ =>` is never even attempted.
49+
50+
```rust,editable
51+
#[rustfmt::skip]
52+
fn main() {
53+
let input = 'a';
54+
match input {
55+
key if key.is_uppercase() => println!("Uppercase"),
56+
key => if input == 'q' { println!("Quitting") },
57+
_ => println!("Bug: this is never printed"),
58+
}
59+
}
60+
```
61+
4862
- The condition defined in the guard applies to every expression in a pattern
4963
with an `|`.
5064
- Note that you can't use an existing variable as the condition in a match arm,

0 commit comments

Comments
 (0)