From d19baf7bae3d9918b6e5554b7534527c1e9f20f6 Mon Sep 17 00:00:00 2001 From: mjrogozinski Date: Sat, 1 Apr 2023 07:22:15 +0200 Subject: [PATCH] Aligning question 7 with bindings_with_variant_name being denied by default --- questions/007-surprise-wildcard-match.md | 61 +----------------------- questions/007-surprise-wildcard-match.rs | 4 +- 2 files changed, 4 insertions(+), 61 deletions(-) diff --git a/questions/007-surprise-wildcard-match.md b/questions/007-surprise-wildcard-match.md index 06d4e15..06cc108 100644 --- a/questions/007-surprise-wildcard-match.md +++ b/questions/007-surprise-wildcard-match.md @@ -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 @@ -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 diff --git a/questions/007-surprise-wildcard-match.rs b/questions/007-surprise-wildcard-match.rs index 3051702..098a262 100644 --- a/questions/007-surprise-wildcard-match.rs +++ b/questions/007-surprise-wildcard-match.rs @@ -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"), } } }