rust_learning/enum_match/
enum_def.rs1enum IpAddrKind {
2 V4,
3 V6,
4}
5
6struct IpAddr {
7 kind: IpAddrKind,
8 address: String,
9}
10
11struct IpAddr2 {
12 kind: IpAddrKind,
13 address: (u8, u8, u8, u8),
14}
15
16enum IpAddress {
17 V4(String),
18 V6(String),
19}
20
21pub fn enum_def() {
23 let four = IpAddrKind::V4;
25 let six = IpAddrKind::V6;
27
28 route(four);
30 route(six);
32 route(IpAddrKind::V6);
34
35
36 let home = IpAddr {
37 kind: IpAddrKind::V4,
38 address: String::from("127.0.0.1"),
39 };
40
41 let loopback = IpAddr {
42 kind: IpAddrKind::V6,
43 address: String::from("::1"),
44 };
45
46
47 let home = IpAddress::V4(String::from("127.0.0.1"));
48 let loopback = IpAddress::V6(String::from("::1"));
49
50 let home = IpAddr2 {
51 kind: IpAddrKind::V4,
52 address: (127, 0, 0, 1),
53 };
54}
55
56
57fn route(ip_type: IpAddrKind) {
58 match ip_type {
59 IpAddrKind::V4 => println!("IPv4"),
60 IpAddrKind::V6 => println!("IPv6"),
61 }
62}
63
64#[derive(Debug)]
70enum Message {
71 Quit,
72 Move { x: i32, y: i32 },
73 Write(String),
74 ChangeColor(i32, i32, i32),
75}
76impl Message {
77 fn call(&self) {
78 println!("{:?}-----Message::call", self)
79 }
80}
81
82pub fn enum_struct() {
84 Message::Quit.call();
85 Message::Move { x: 1, y: 2 }.call();
86 Message::Write(String::from("hello")).call();
87 Message::ChangeColor(1, 2, 3).call();
88}