rust-by-practice/en/src/pattern-match/patterns.md

120 lines
2.9 KiB
Markdown
Raw Normal View History

# Patterns
2022-03-01 06:12:55 -06:00
2022-05-04 04:12:49 -05:00
1. 🌟🌟 Use `|` to match several values, use `..=` to match an inclusive range.
2022-03-01 06:12:55 -06:00
```rust,editable
fn main() {}
fn match_number(n: i32) {
match n {
2022-05-04 04:12:49 -05:00
// Match a single value
2022-03-01 06:12:55 -06:00
1 => println!("One!"),
2022-05-04 04:12:49 -05:00
// Fill in the blank with `|`, DON'T use `..` or `..=`
2022-03-01 06:12:55 -06:00
__ => println!("match 2 -> 5"),
2022-05-04 04:12:49 -05:00
// Match an inclusive range
2022-03-01 06:12:55 -06:00
6..=10 => {
println!("match 6 -> 10")
},
_ => {
2022-10-08 14:39:44 -05:00
println!("match -infinite -> 0 or 11 -> +infinite")
2022-03-01 06:12:55 -06:00
}
}
}
```
2022-05-04 04:12:49 -05:00
2. 🌟🌟🌟 The `@` operator lets us create a variable that holds a value, at the same time we are testing that value to see whether it matches a pattern.
2022-03-01 06:55:22 -06:00
```rust,editable
struct Point {
x: i32,
y: i32,
}
fn main() {
2022-05-04 04:12:49 -05:00
// Fill in the blank to let p match the second arm
2022-03-01 06:55:22 -06:00
let p = Point { x: __, y: __ };
match p {
Point { x, y: 0 } => println!("On the x axis at {}", x),
2022-05-04 04:12:49 -05:00
// Second arm
2022-03-01 06:55:22 -06:00
Point { x: 0..=5, y: y@ (10 | 20 | 30) } => println!("On the y axis at {}", y),
Point { x, y } => println!("On neither axis: ({}, {})", x, y),
}
}
```
2022-03-02 08:04:00 -06:00
3. 🌟🌟🌟
2022-03-01 06:55:22 -06:00
```rust,editable
2022-05-04 04:12:49 -05:00
// Fix the errors
2022-03-01 06:55:22 -06:00
enum Message {
Hello { id: i32 },
}
fn main() {
let msg = Message::Hello { id: 5 };
match msg {
Message::Hello {
id: 3..=7,
} => println!("Found an id in range [3, 7]: {}", id),
Message::Hello { id: newid@10 | 11 | 12 } => {
println!("Found an id in another range [10, 12]: {}", newid)
}
Message::Hello { id } => println!("Found some other id: {}", id),
}
}
```
2022-03-02 08:04:00 -06:00
4. 🌟🌟 A match guard is an additional if condition specified after the pattern in a match arm that must also match, along with the pattern matching, for that arm to be chosen.
2022-03-01 06:55:22 -06:00
```rust,editable
2022-05-04 04:12:49 -05:00
// Fill in the blank to make the code work, `split` MUST be used
2022-03-01 06:55:22 -06:00
fn main() {
let num = Some(4);
let split = 5;
match num {
Some(x) __ => assert!(x < split),
Some(x) => assert!(x >= split),
None => (),
}
2022-05-04 04:12:49 -05:00
println!("Success!");
2022-03-01 06:55:22 -06:00
}
```
2022-03-02 08:04:00 -06:00
5. 🌟🌟 Ignoring remaining parts of the value with `..`
2022-03-01 06:55:22 -06:00
```rust,editable
2022-05-04 04:12:49 -05:00
// Fill the blank to make the code work
2022-03-01 06:55:22 -06:00
fn main() {
let numbers = (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048);
match numbers {
__ => {
assert_eq!(first, 2);
assert_eq!(last, 2048);
}
}
2022-05-04 04:12:49 -05:00
println!("Success!");
2022-03-01 06:55:22 -06:00
}
2022-03-01 08:06:38 -06:00
```
2022-05-04 04:12:49 -05:00
6. 🌟🌟 Using pattern `&mut V` to match a mutable reference needs you to be very careful, due to `V` being a value after matching.
2022-03-11 01:45:03 -06:00
```rust,editable
// FIX the error with least changing
// DON'T remove any code line
fn main() {
let mut v = String::from("hello,");
let r = &mut v;
match r {
&mut value => value.push_str(" world!")
}
}
2022-10-10 22:11:06 -05:00
```
2022-03-11 01:45:03 -06:00
2022-10-08 14:39:44 -05:00
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it