I want to make an enum from a struct.
The definitions:
struct Point {
x: u8,
y: u8,
}
enum Message {
Move { x: u8, y: u8 },
nothing,
}
So if I have instantiated a Point in my code, how can I make a Message from the struct?
I’ve tried this code:
Message::Move(Point { x: 10, y: 15 })
But this code throws an error saying
error[E0423]: expected function, tuple struct or tuple variant, found struct variant `Message::Move`
>Solution :
Your enum variant can simply contain an instance of the struct.
enum Message {
Move(Point),
NoMessage,
}
Then the syntax you wanted will work as-is.
Message::Move(Point { x: 10, y: 15 })