Merge pull request #3894 from rust-lang/revert-3397-ch18-03-pattern-syntax.md

Revert "ch18-03: Guarded match arm exhaustivness clarification"
This commit is contained in:
Chris Krycho 2024-04-18 16:34:49 -06:00 committed by GitHub
commit f84626dbe0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 2 additions and 21 deletions

View File

@ -477,27 +477,8 @@ second arm doesnt 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 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.
this additional expressiveness is that the compiler doesn't try to check for
exhaustiveness when match guard expressions are involved.
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