mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-07-20 11:25:31 +00:00
update repo layout
This commit is contained in:
6
en/src/pattern-match/intro.md
Normal file
6
en/src/pattern-match/intro.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Pattern Match
|
||||
Learning resources:
|
||||
- English: [Rust Book 18](https://doc.rust-lang.org/book/ch18-00-patterns.html)
|
||||
- 简体中文: [Rust语言圣经 - 模式匹配](https://course.rs/basic/match-pattern/intro.html)
|
||||
|
||||
|
210
en/src/pattern-match/match-iflet.md
Normal file
210
en/src/pattern-match/match-iflet.md
Normal file
@@ -0,0 +1,210 @@
|
||||
# match, if let
|
||||
|
||||
### match
|
||||
1. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// fill the blanks
|
||||
enum Direction {
|
||||
East,
|
||||
West,
|
||||
North,
|
||||
South,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let dire = Direction::South;
|
||||
match dire {
|
||||
Direction::East => println!("East"),
|
||||
__ => { // matching South or North here
|
||||
println!("South or North");
|
||||
},
|
||||
_ => println!(__),
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
2. 🌟🌟 match is an expression, so we can use it in assignments
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let boolean = true;
|
||||
|
||||
// fill the blank with an match expression:
|
||||
//
|
||||
// boolean = true => binary = 1
|
||||
// boolean = false => binary = 0
|
||||
let binary = __;
|
||||
|
||||
assert_eq!(binary, 1);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
3. 🌟🌟 using match to get the data an enum variant holds
|
||||
```rust,editable
|
||||
|
||||
// fill in the blanks
|
||||
enum Message {
|
||||
Quit,
|
||||
Move { x: i32, y: i32 },
|
||||
Write(String),
|
||||
ChangeColor(i32, i32, i32),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let msgs = [
|
||||
Message::Quit,
|
||||
Message::Move{x:1, y:3},
|
||||
Message::ChangeColor(255,255,0)
|
||||
];
|
||||
|
||||
for msg in msgs {
|
||||
show_message(msg)
|
||||
}
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
|
||||
fn show_message(msg: Message) {
|
||||
match msg {
|
||||
__ => { // match Message::Move
|
||||
assert_eq!(a, 1);
|
||||
assert_eq!(b, 3);
|
||||
},
|
||||
Message::ChangeColor(_, g, b) => {
|
||||
assert_eq!(g, __);
|
||||
assert_eq!(b, __);
|
||||
}
|
||||
__ => println!("no data in these variants")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### matches!
|
||||
[`matches!`](https://doc.rust-lang.org/stable/core/macro.matches.html) looks like `match`, but can do something different
|
||||
|
||||
4. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];
|
||||
|
||||
// fill the blank with `matches!` to make the code work
|
||||
for ab in alphabets {
|
||||
assert!(__)
|
||||
}
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
5. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
enum MyEnum {
|
||||
Foo,
|
||||
Bar
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut count = 0;
|
||||
|
||||
let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo];
|
||||
for e in v {
|
||||
if e == MyEnum::Foo { // fix the error with changing only this line
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(count, 2);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
### if let
|
||||
For some cases, when matching enums, `match` is too heavy, we can use `if let` instead.
|
||||
|
||||
6. 🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
let o = Some(7);
|
||||
|
||||
// remove the whole `match` block, using `if let` instead
|
||||
match o {
|
||||
Some(i) => {
|
||||
println!("This is a really long string and `{:?}`", i);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
7. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// fill in the blank
|
||||
enum Foo {
|
||||
Bar(u8)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Foo::Bar(1);
|
||||
|
||||
__ {
|
||||
println!("foobar holds the value: {}", i);
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
8. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
enum Foo {
|
||||
Bar,
|
||||
Baz,
|
||||
Qux(u32)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Foo::Qux(10);
|
||||
|
||||
// remove the codes below, using `match` instead
|
||||
if let Foo::Bar = a {
|
||||
println!("match foo::bar")
|
||||
} else if let Foo::Baz = a {
|
||||
println!("match foo::baz")
|
||||
} else {
|
||||
println!("match others")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Shadowing
|
||||
9. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// fix the errors in-place
|
||||
fn main() {
|
||||
let age = Some(30);
|
||||
if let Some(age) = age { // create a new variable with the same name as previous `age`
|
||||
assert_eq!(age, Some(30));
|
||||
} // the new variable `age` goes out of scope here
|
||||
|
||||
match age {
|
||||
// match can also introduce a new shadowed variable
|
||||
Some(age) => println!("age is a new variable, it's value is {}",age),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
> 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
|
119
en/src/pattern-match/patterns.md
Normal file
119
en/src/pattern-match/patterns.md
Normal file
@@ -0,0 +1,119 @@
|
||||
# Patterns
|
||||
|
||||
1. 🌟🌟 use `|` to match several values, use `..=` to match a inclusive range
|
||||
```rust,editable
|
||||
|
||||
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 `..=`
|
||||
__ => println!("match 2 -> 5"),
|
||||
// match an inclusive range
|
||||
6..=10 => {
|
||||
println!("match 6 -> 10")
|
||||
},
|
||||
_ => {
|
||||
println!("match 11 -> +infinite")
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// fill in the blank to let p match the second arm
|
||||
let p = Point { x: __, y: __ };
|
||||
|
||||
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,editable
|
||||
|
||||
// fix the errors
|
||||
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),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
// fill in the blank to make the code work, `split` MUST be used
|
||||
fn main() {
|
||||
let num = Some(4);
|
||||
let split = 5;
|
||||
match num {
|
||||
Some(x) __ => assert!(x < split),
|
||||
Some(x) => assert!(x >= split),
|
||||
None => (),
|
||||
}
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
5. 🌟🌟 Ignoring remaining parts of the value with `..`
|
||||
```rust,editable
|
||||
|
||||
// fill the blank to make the code work
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
println!("Success!")
|
||||
}
|
||||
```
|
||||
|
||||
6. 🌟🌟 Using pattern `&mut V` to match a mutable reference needs you to be very careful due to `V` being a value after matching
|
||||
```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!")
|
||||
}
|
||||
}
|
||||
````
|
||||
|
||||
> 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
|
Reference in New Issue
Block a user