Time：2021-9-18

# Explanation content

1. Match by
Example 1:

``````let x = 5;
match x {
1..=5 => println! ("one through five"), // that is, the range from 1 to 5 is matched
_ => println!("something else"),
}``````

Note: in the example, 1.. = 5 is equivalent to 1 | 2 | 3 | 4 | 5
Example 2:

``````let x = 'c';
match x {
'a'..='j' => println!("early ASCII letter"),
'k'..='z' => println!("late ASCII letter"),
_ => println!("something else"),
}``````

2. Deconstruct and decompose values
You can use patterns to deconstruct structs, enumerations, tuples, and references to use different parts of these values.
(1) Deconstruction structure
example:

``````struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 0, y: 7 };
let Point { x: a, y: b } = p;
assert_eq!(0, a);
assert_eq!(7, b);
//It can also be written as:
//let Point { x, y } = p;   // A variable with the same name is created, which can be abbreviated
//assert_eq!(0, x);
//assert_eq!(7, y);
}``````

Description: variables A and B are used to match the X and Y fields in structure P.

Instead of creating variables for all fields, you can also deconstruct using literals as part of the structure pattern. Examples are as follows:

``````fn main() {
let p = Point { x: 0, y: 7 };
match p {
Point { x, y: 0 } => println!("On the x axis at {}", x),
Point { x: 0, y } => println!("On the y axis at {}", y),
Point { x, y } => println!("On neither axis: ({}, {})", x, y),
}
}``````

(2) Deconstruct enumeration type
Example (review):

``````enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
fn main() {
let msg = Message::ChangeColor(0, 160, 255);
match msg {
Message::Quit => {
println!("The Quit variant has no data to destructure.")
}
Message::Move { x, y } => {
println!(
"Move in the x direction {} and in the y direction {}",
x,
y
);
}
Message::Write(text) => println!("Text message: {}", text),
Message::ChangeColor(r, g, b) => {
println!(
"Change the color to red {}, green {}, and blue {}",
r,
g,
b
)
}
}
}``````

explain:
For enumeration members without any data such as message:: quit, their values cannot be further deconstructed. Can only match its literal value message:: quit;
For enumerating members of class structures such as message:: move, a pattern similar to matching structures can be adopted;
For class tuple enumeration members such as message:: write containing one element and message:: changecolor containing two elements, the pattern is similar to that used to deconstruct tuples.

(3) Deconstructing nested structures and enumerations
example:

``````enum Color {
Rgb(i32, i32, i32),
Hsv(i32, i32, i32),
}

enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(Color),
}

fn main() {
let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));

match msg {
Message::ChangeColor(Color::Rgb(r, g, b)) => {
println!(
"Change the color to red {}, green {}, and blue {}",
r,
g,
b
)
}
Message::ChangeColor(Color::Hsv(h, s, v)) => {
println!(
"Change the color to hue {}, saturation {}, and value {}",
h,
s,
v
)
}
_ => ()
}
}``````

(4) Deconstruction structure and tuple
example:

``````struct Point{
x: i32,
y: i32,
}
fn main() {
let ((feet, inches), Point {x, y}) = ((3, 10), Point { x: 3, y: -10 });
println!("feet = {}, inches = {}, x = {}, y = {}", feet, inches, x, y);
println!("Hello, world!");
}``````