fix errors in enum.md

This commit is contained in:
sunface
2022-03-01 18:58:36 +08:00
parent bda6040eab
commit 726ef682cb
8 changed files with 144 additions and 19 deletions

View File

@ -1 +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)

View File

@ -1 +1,119 @@
# match, if let
### match
🌟🌟
```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!(__),
};
}
```
🌟🌟 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);
}
```
🌟🌟 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)
}
}
fn show_message(msg: Message) {
match msg {
__ => { // matches 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
🌟🌟
```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!(__)
}
}
```
🌟🌟
```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);
}
```
### if let