Skip to content

Aligning question 7 with deny(bindings_with_variant_name) #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 2 additions & 59 deletions questions/007-surprise-wildcard-match.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Answer: 1
Answer: 2
Difficulty: 2
Warnings: bindings_with_variant_name, dead_code, non_snake_case, unreachable_patterns, unused_variables
Warnings: dead_code, non_snake_case, unreachable_patterns

# Hint

Expand All @@ -24,60 +24,3 @@ that our type has the same representation as `u8`, and the discriminant on
`Enum::Second` guarantees that `Enum::Second` has the same representation as
`1u8`. The transmute is well-defined and evaluates to `Enum::Second`.

If the method `p` had been written as:

```rust
match self {
Enum::First => print!("1"),
Enum::Second => print!("2"),
}
```

then this program would print `2`.

However, as written, both arms of the `match` expression are wildcard matches
that successfully match *any* value and bind a *variable* with the name `First`
or `Second`. Match arms are applied in order so the wildcard match in the first
arm is always the one matched.

The compiler helps us out with not one but two relevant warnings. First it
describes exactly how this `match` is parsed and why that is probably silly.

```
warning: unreachable pattern
--> questions/007.rs:11:13
|
10 | First => print!("1"),
| ----- matches any value
11 | Second => print!("2"),
| ^^^^^^ unreachable pattern
```

Second, it recognizes what the programmer has done wrong and what they probably
meant to write instead.

```
warning[E0170]: pattern binding `First` is named the same as one of the variants of the type `Enum`
--> questions/007.rs:10:13
|
10 | First => print!("1"),
| ^^^^^ help: to match on the variant, qualify the path: `Enum::First`
```

An alternative to writing qualified paths in the pattern is to bring the
variants into scope.

```rust
use Enum::*;

match self {
First => print!("1"),
Second => print!("2"),
}
```

Having variants brought into scope by the [standard library prelude][prelude] is
what allows us to write `Ok` and `Some` in match arms, rather than the qualified
paths `Result::Ok` and `Option::Some`.

[prelude]: https://doc.rust-lang.org/std/prelude/index.html
4 changes: 2 additions & 2 deletions questions/007-surprise-wildcard-match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ enum Enum {
impl Enum {
fn p(self) {
match self {
First => print!("1"),
Second => print!("2"),
Enum::First => print!("1"),
Enum::Second => print!("2"),
}
}
}
Expand Down