study_example/pattern_match/
pattern_syntax.rs1fn 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
15fn match_named_variable() {
21 let x = Some(10);
22 let y = 5;
23 match x {
24 Some(50) => println!("Got 50"),
25 Some(y) => println!("Matched, y = {y}"),
28 _ => println!("Default case, x = {:?}", x),
29 }
30 println!("end, x = {:?}, y = {y}", x);
31}
32
33fn 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
46fn 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
63fn 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
76fn 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
102fn 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
122fn test_ignore_params(_: i32, y: i32) {
127 println!("this code only use the y params: {y}");
128}
129
130fn 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
164fn 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
180fn 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}