Enums
An enum is one of several variants, which may carry data.
Example
enum Msg { Quit, Move { x: i32, y: i32 }, Write(String) }
fn main() {
let m = [Msg::Quit, Msg::Move { x: 1, y: 2 }, Msg::Write("hi".into())];
for x in &m {
match x {
Msg::Quit => println!("quit"),
Msg::Move { x, y } => println!("move {} {}", x, y),
Msg::Write(s) => println!("write {}", s),
}
}
}Result
quit move 1 2 write hi
Exercise
Enum keyword.
Msg {}Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.