enum: Add full example with match - access specific type params
This commit is contained in:
parent
365f525e57
commit
086a603bab
1 changed files with 14 additions and 1 deletions
|
|
@ -17,7 +17,16 @@ enum Message {
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
fn print(&self) {
|
fn print(&self) {
|
||||||
println!("calling message {:?}", self);
|
println!("I don't do anything but can be called with any type: {:?}", self);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&self) {
|
||||||
|
match self {
|
||||||
|
Message::Quit => println!("Calling on a quit type!"),
|
||||||
|
Message::Move { x, y } => println!("Calling on a move type: {}, {}!", x, y),
|
||||||
|
Message::Write (s) => println!("Calling on a write type: {}!", s),
|
||||||
|
Message::ChangeColor (c) => println!("Calling on a color type: {:#?}!", c),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -29,10 +38,14 @@ fn main() {
|
||||||
|
|
||||||
let msg = Message::Quit;
|
let msg = Message::Quit;
|
||||||
msg.print();
|
msg.print();
|
||||||
|
msg.call();
|
||||||
let msg = Message::Move{ x: 1, y:2};
|
let msg = Message::Move{ x: 1, y:2};
|
||||||
msg.print();
|
msg.print();
|
||||||
|
msg.call();
|
||||||
let msg = Message::Write(String::from("Hello enum"));
|
let msg = Message::Write(String::from("Hello enum"));
|
||||||
msg.print();
|
msg.print();
|
||||||
|
msg.call();
|
||||||
let msg = Message::ChangeColor(Color(1,2,3));
|
let msg = Message::ChangeColor(Color(1,2,3));
|
||||||
msg.print();
|
msg.print();
|
||||||
|
msg.call();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue