-
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? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There are two ways to use your own struct inside an enum. The first one is to have an "anonymous struct" like the following: enum Message {
Move { x: u64, y: u64 },
// …
} The second one is to have a named struct defined separately (what you want for struct Point {
x: u64,
y: u64,
}
enum Message {
Move(Point),
// …
} The updated exercise wants you to practice both variants since |
Beta Was this translation helpful? Give feedback.
There are two ways to use your own struct inside an enum.
The first one is to have an "anonymous struct" like the following:
The second one is to have a named struct defined separately (what you want for
Move
):The updated exercise wants you to practice both variants since
Resize
has the first one.