study_example/pattern_match/
pattern_syntax.rs

1/// 运行结果如下
2/// ```txt
3/// two
4/// ```
5fn match_literals() {
6    let x = 2;
7    match x {
8        1 => println!("one"),
9        2 => println!("two"),
10        3 => println!("three"),
11        _ => println!("anything"),
12    }
13}
14
15/// 运行结果如下
16/// ```txt
17/// Matched, y = 10
18/// end, x = Some(10), y = 5
19/// ```
20fn match_named_variable() {
21    let x = Some(10);
22    let y = 5;
23    match x {
24        Some(50) => println!("Got 50"),
25        // 引入了一个名为 y 的新变量,它将匹配 Some 值内的任何值。因为我们处于 match 表达式内的新作用域,所以这是一个新的 y 变量,而不是我们在开头声明的值为 10 的 y
26        // 这个新的 y 绑定将匹配 Some 内的任何值,这就是我们在 x 中的值。因此,这个新的 y 绑定到 x 中 Some 的内部值
27        Some(y) => println!("Matched, y = {y}"),
28        _ => println!("Default case, x = {:?}", x),
29    }
30    println!("end, x = {:?}, y = {y}", x);
31}
32
33/// 运行结果如下
34/// ```txt
35/// one or two
36/// ```
37fn multi_matched() {
38    let x = 2;
39    match x {
40        1 | 2 => println!("one or two"),
41        3 => println!("three"),
42        _ => println!("anything."),
43    }
44}
45
46/// 运行结果如下
47/// ```txt
48/// from zero to five
49/// ```
50fn match_range_vals() {
51    let x = 4;
52    match x {
53        0..=5 => println!("from zero to five"),
54        _ => println!("something else"),
55    }
56}
57
58struct Point {
59    x: i32,
60    y: i32,
61}
62
63/// 运行结果如下
64/// ```txt
65/// a = 0, b = 7
66/// x = 0, y = 7
67/// ```
68fn destruct_struct() {
69    let p = Point { x: 0, y: 7 };
70    let Point { x: a, y: b } = p;
71    let Point { x, y } = p;
72    println!("a = {a}, b = {b}");
73    println!("x = {x}, y = {y}");
74}
75
76/// 运行结果如下
77/// ```txt
78/// on the y axis at 7
79/// ```
80fn match_point() {
81    let p = Point { x: 0, y: 7 };
82    match p {
83        Point { x, y: 0 } => println!("on the x axis at {x}"),
84        Point { x: 0, y } => println!("on the y axis at {y}"),
85        Point { x, y } => {
86            println!("on neither axis :({x}, {y})")
87        }
88    }
89}
90
91enum Color {
92    Rgb(i32, i32, i32),
93    Hsv(i32, i32, i32),
94}
95enum Message {
96    Quit,
97    Move { x: i32, y: i32 },
98    Write(String),
99    ChangeColor(Color),
100}
101
102/// 运行结果如下
103/// ```txt
104/// Change color to hue 0, saturation 160, value 255
105/// ```
106fn match_nested_struct_enum() {
107    let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));
108    match msg {
109        Message::ChangeColor(Color::Rgb(r, g, b)) => {
110            println!("Change color to red {r}, green {g}, blue {b}");
111        }
112        Message::ChangeColor(Color::Hsv(h, s, v)) => {
113            println!("Change color to hue {h}, saturation {s}, value {v}");
114        }
115        Message::Move { x, y } => {
116            println!("move x: {x}, y: {y}");
117        }
118        _ => (),
119    }
120}
121
122/// 运行结果如下
123/// ```txt
124/// this code only use the y params: 4  
125/// ```
126fn test_ignore_params(_: i32, y: i32) {
127    println!("this code only use the y params: {y}");
128}
129
130/// 运行结果如下
131/// ```txt
132/// Can't override an existing customized value
133/// setting is Some(5)
134/// _ ignore case: Some numbers: 1, 3
135/// .. ignore case: Some numbers: 1, 7
136/// ```
137fn test_ignore_part_value() {
138    let mut setting_value = Some(5);
139    let new_setting_value = Some(10);
140    match (setting_value, new_setting_value) {
141        (Some(_), Some(_)) => {
142            println!("Can't override an existing customized value");
143        }
144        _ => {
145            setting_value = new_setting_value;
146        }
147    }
148    println!("setting is {:?}", setting_value);
149
150    let numbers = (1, 2, 3, 4, 5, 6, 7);
151    match numbers {
152        (first, _, third, _, _, _, _) => {
153            println!("_ ignore case: Some numbers: {first}, {third}");
154        }
155    }
156
157    match numbers {
158        (first, .., last) => {
159            println!(".. ignore case: Some numbers: {first}, {last}");
160        }
161    }
162}
163
164/// 运行结果如下
165/// ```txt
166/// Matched, n = 10
167/// end: x = Some(10) y = 10
168/// ```
169fn match_guard() {
170    let x = Some(10);
171    let y = 10;
172    match x {
173        Some(50) => println!("Got 50"),
174        Some(n) if n == y => println!("Matched, n = {n}"),
175        _ => println!("Default case, x = {:?}", x),
176    }
177    println!("end: x = {:?} y = {y}", x)
178}
179
180/// 运行结果如下
181/// ```txt
182/// found y in range: 7, x = 34
183/// ```
184fn match_bindings() {
185    let msg = Message::Move { x: 34, y: 7 };
186    match msg {
187        Message::Move {
188            x,
189            y: y_var @ 3..=9,
190        } => println!("found y in range: {}, x = {}", y_var, x),
191        _ => (),
192    }
193}
194
195pub fn pattern_syntax_study() {
196    match_literals();
197    match_named_variable();
198    multi_matched();
199    match_range_vals();
200    destruct_struct();
201    match_point();
202    match_nested_struct_enum();
203    test_ignore_params(3, 4);
204    test_ignore_part_value();
205    match_guard();
206    match_bindings();
207}