Skip to content

Commit

Permalink
Merge pull request #3397 from bravequickcleverfibreyarn/ch18-03-patte…
Browse files Browse the repository at this point in the history
…rn-syntax.md

ch18-03: Guarded match arm exhaustivness clarification
  • Loading branch information
chriskrycho authored Apr 18, 2024
2 parents 18effd3 + 7f49519 commit 49157ed
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/ch18-03-pattern-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,27 @@ second arm doesn’t have a match guard and therefore matches any `Some` variant

There is no way to express the `if x % 2 == 0` condition within a pattern, so
the match guard gives us the ability to express this logic. The downside of
this additional expressiveness is that the compiler doesn't try to check for
exhaustiveness when match guard expressions are involved.
this additional expressiveness is that the compiler is not smart enough about
arms exhaustiveness anymore when match guard expressions are involved.

```rust,ignore,does_not_compile
match Some(100) {
Some(_) if true => print!("Got Some"),
None => print!("Got None")
}
```
This `match` expression involves compilation error.
```console
error[E0004]: non-exhaustive patterns: `Some(_)` not covered
--> src/main.rs:33:11
|
33 | match Some(100) {
| ^^^^^^^^^ pattern `Some(_)` not covered
|
```
Compilation fails even though `Some(_) if true` guarded pattern matches
any possible `Some`. Same as unguarded `Some(_)` does.


In Listing 18-11, we mentioned that we could use match guards to solve our
pattern-shadowing problem. Recall that we created a new variable inside the
Expand Down

0 comments on commit 49157ed

Please sign in to comment.