Skip to content
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

Question 7 requires correction. #71

Closed
mjrogozinski opened this issue Apr 1, 2023 · 1 comment · Fixed by #74
Closed

Question 7 requires correction. #71

mjrogozinski opened this issue Apr 1, 2023 · 1 comment · Fixed by #74

Comments

@mjrogozinski
Copy link

mjrogozinski commented Apr 1, 2023

In question 7, the original code looks like this:

#[repr(u8)]
enum Enum {
    First,
    Second,
}

impl Enum {
    fn p(self) {
        match self {
            First => print!("1"),
            Second => print!("2"),
        }
    }
}

fn main() {
    Enum::p(unsafe {
        std::mem::transmute(1u8)
    });
}

We get compilation errors on lines

            First => print!("1"),
            Second => print!("2"),
error[E0170]: pattern binding `First` is named the same as one of the variants of the type `Enum`
  --> src/main.rs:10:13
   |
10 |             First => print!("1"),
   |             ^^^^^ help: to match on the variant, qualify the path: `Enum::First`
   |
   = note: `#[deny(bindings_with_variant_name)]` on by default

error[E0170]: pattern binding `Second` is named the same as one of the variants of the type `Enum`
  --> src/main.rs:11:13
   |
11 |             Second => print!("2"),
   |             ^^^^^^ help: to match on the variant, qualify the path: `Enum::Second`

after fixing them by adding Enum::

            Enum::First => print!("1"),
            Enum::Second => print!("2"),

the output is "2".

The answer matches the description, we get "1" for 0u8 and "2" for 1u8. It does not, however, match the "any" part nor the official answer stating the program would print "1".

Below, there are two possible ways for fixing the issue.
Either Enum:: should be added to the code and the answer changed to "2" or the answer should be changed to "The program does not compile".

@mjrogozinski
Copy link
Author

It's probably related to rust-lang/rust#104154

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant