rust-lang / rustlings

:crab: Small exercises to get you used to reading and writing Rust code!
https://rustlings.cool
MIT License
52.69k stars 10.02k forks source link

I'm having trouble at enums2.rs #2078

Closed nasccped closed 1 month ago

nasccped commented 1 month ago

I'm new at Rust language and I'm having a lot of fun with the lang, rustlings and ytb videos, but, doing some rustlings exercises, i became stucked at enums2.rs.


I'll try to be as brief as possible:

I spent a time looking at enums2.rs solutions on gitHub Gist and I realize that the solutions was made through an older rustlings version.

On the link marked above, we have something like this:

#[derive(Debug)]
enum Message {
    // TODO: define the different variants used below
}

impl Message {
    fn call(&self) {
        println!("{:?}", &self);
    }
}

fn main() {
    let messages = [
        Message::Move{ x: 10, y: 30 }, // NOTE: that line it's important <<<
        Message::Echo(String::from("hello world")),
        Message::ChangeColor(200, 255, 255),
        Message::Quit
    ];

    for message in &messages {
        message.call();
    }
}

But, in my local rustlings, i have something like:

fn main() {
    let messages = [
        Message::Resize {
            width: 10,
            height: 30,
        },
        Message::Move(Point { x: 10, y: 15 }), // NOTE: is exactly this line <<<
        Message::Echo(String::from("hello world")),
        Message::ChangeColor(200, 255, 255),
        Message::Quit,
    ];

    for message in &messages {
        message.call();
    }
}

To solve the exercise, i did:

#[derive(Debug)]
enum Message {
    // TODO: Define the different variants used below.
    Resize { width: i32, height: i32 },
    Move( Point { x: i32, y: i32 } ),
    Echo(String),
    ChangeColor(i32, i32, i32),
    Quit
}

but, when running the program, I've got:

error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `{`
  --> exercises/08_enums/enums2.rs:13:17
   |
10 | enum Message {
   |      ------- while parsing this enum
...
13 |     Move( Point { x: i32, y: i32 } ),
   |                 ^ expected one of 7 possible tokens
   |
   = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`

error[E0061]: this enum variant takes 0 arguments but 1 argument was supplied
  --> exercises/08_enums/enums2.rs:31:9
   |
31 |         Message::Move(Point { x: 10, y: 15 }), // NOTE: is exactly this line <<<
   |         ^^^^^^^^^^^^^ ----------------------
   |                       |
   |                       unexpected argument of type `Point`
   |                       help: remove the extra argument
   |
note: tuple variant defined here
  --> exercises/08_enums/enums2.rs:13:5
   |
13 |     Move( Point { x: i32, y: i32 } ),
   |     ^^^^

For more information about this error, try `rustc --explain E0061`.
error: could not compile `exercises` (bin "enums2") due to 2 previous errors

The way that I think doesn't seem to be wrong. I know exactly what i need to do to beat the task, but that doesn't seem like the way I should do it. Can someone help me?