enum IpAddrKind {
V4,
V6,
}
struct IpAddr {
kind: IpAddrKind,
address: String,
}
struct IpAddr2 {
kind: IpAddrKind,
address: (u8, u8, u8, u8),
}
enum IpAddress {
V4(String),
V6(String),
}
pub fn enum_def() {
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
route(four);
route(six);
route(IpAddrKind::V6);
let home = IpAddr {
kind: IpAddrKind::V4,
address: String::from("127.0.0.1"),
};
let loopback = IpAddr {
kind: IpAddrKind::V6,
address: String::from("::1"),
};
let home = IpAddress::V4(String::from("127.0.0.1"));
let loopback = IpAddress::V6(String::from("::1"));
let home = IpAddr2 {
kind: IpAddrKind::V4,
address: (127, 0, 0, 1),
};
}
fn route(ip_type: IpAddrKind) {
match ip_type {
IpAddrKind::V4 => println!("IPv4"),
IpAddrKind::V6 => println!("IPv6"),
}
}
#[derive(Debug)]
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
impl Message {
fn call(&self) {
println!("{:?}-----Message::call", self)
}
}
pub fn enum_struct() {
Message::Quit.call();
Message::Move { x: 1, y: 2 }.call();
Message::Write(String::from("hello")).call();
Message::ChangeColor(1, 2, 3).call();
}