mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
add solutions for patterns.md
This commit is contained in:
@ -0,0 +1,88 @@
|
|||||||
|
1.
|
||||||
|
```rust
|
||||||
|
fn main() {}
|
||||||
|
fn match_number(n: i32) {
|
||||||
|
match n {
|
||||||
|
// match a single value
|
||||||
|
1 => println!("One!"),
|
||||||
|
// fill in the blank with `|`, DON'T use `..` ofr `..=`
|
||||||
|
2 | 3 | 4 | 5 => println!("match 2 -> 5"),
|
||||||
|
// match an inclusive range
|
||||||
|
6..=10 => {
|
||||||
|
println!("match 6 -> 10")
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
println!("match 11 -> +infinite")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
2.
|
||||||
|
```rust
|
||||||
|
|
||||||
|
struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// fill in the blank to let p match the second arm
|
||||||
|
let p = Point { x: 2, y: 20 }; // x can be [0, 5], y can be 10 20 or 30
|
||||||
|
|
||||||
|
match p {
|
||||||
|
Point { x, y: 0 } => println!("On the x axis at {}", x),
|
||||||
|
// second arm
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
3.
|
||||||
|
```rust
|
||||||
|
enum Message {
|
||||||
|
Hello { id: i32 },
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let msg = Message::Hello { id: 5 };
|
||||||
|
|
||||||
|
match msg {
|
||||||
|
Message::Hello {
|
||||||
|
id: 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
4.
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let num = Some(4);
|
||||||
|
let split = 5;
|
||||||
|
match num {
|
||||||
|
Some(x) if x < split => assert!(x < split),
|
||||||
|
Some(x) => assert!(x >= split),
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
5.
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let numbers = (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048);
|
||||||
|
|
||||||
|
match numbers {
|
||||||
|
(first,..,last) => {
|
||||||
|
assert_eq!(first, 2);
|
||||||
|
assert_eq!(last, 2048);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
@ -1,6 +1,6 @@
|
|||||||
# Patterns
|
# Patterns
|
||||||
|
|
||||||
🌟🌟 use `|` to match several values, use `..=` to match a inclusive range
|
1. 🌟🌟 use `|` to match several values, use `..=` to match a inclusive range
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
@ -21,7 +21,7 @@ fn match_number(n: i32) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟🌟 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.
|
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.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
@ -42,7 +42,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟🌟
|
3. 🌟🌟🌟
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟 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.
|
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.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the blank to make the code work, `split` MUST be used
|
// fill in the blank to make the code work, `split` MUST be used
|
||||||
@ -81,7 +81,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟 Ignoring remaining parts of the value with `..`
|
5. 🌟🌟 Ignoring remaining parts of the value with `..`
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill the blank to make the code work
|
// fill the blank to make the code work
|
||||||
|
Reference in New Issue
Block a user