enum: Add more complex type
This commit is contained in:
parent
307c5c1e39
commit
365f525e57
1 changed files with 26 additions and 0 deletions
|
|
@ -4,9 +4,35 @@ enum IpAddrKind {
|
||||||
V6(String),
|
V6(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Color(i32, i32, i32);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Message {
|
||||||
|
Quit,
|
||||||
|
Move { x: i32, y: i32},
|
||||||
|
Write(String),
|
||||||
|
ChangeColor(Color),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Message {
|
||||||
|
fn print(&self) {
|
||||||
|
println!("calling message {:?}", self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let v4 = IpAddrKind::V4(127, 0, 0, 1);
|
let v4 = IpAddrKind::V4(127, 0, 0, 1);
|
||||||
let v6 = IpAddrKind::V6(String::from("::1"));
|
let v6 = IpAddrKind::V6(String::from("::1"));
|
||||||
|
|
||||||
println!("Ipv4: {:#?}, Ipv6: {:#?}", v4, v6);
|
println!("Ipv4: {:#?}, Ipv6: {:#?}", v4, v6);
|
||||||
|
|
||||||
|
let msg = Message::Quit;
|
||||||
|
msg.print();
|
||||||
|
let msg = Message::Move{ x: 1, y:2};
|
||||||
|
msg.print();
|
||||||
|
let msg = Message::Write(String::from("Hello enum"));
|
||||||
|
msg.print();
|
||||||
|
let msg = Message::ChangeColor(Color(1,2,3));
|
||||||
|
msg.print();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue