2022-05-04 03:32:32 -05:00
|
|
|
# Match, if let
|
2022-03-01 04:58:36 -06:00
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
### Match
|
2022-03-02 07:59:22 -06:00
|
|
|
1. ππ
|
2022-03-01 04:58:36 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
// Fill the blanks
|
2022-03-01 04:58:36 -06:00
|
|
|
enum Direction {
|
|
|
|
East,
|
|
|
|
West,
|
|
|
|
North,
|
|
|
|
South,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let dire = Direction::South;
|
|
|
|
match dire {
|
|
|
|
Direction::East => println!("East"),
|
2022-05-04 03:32:32 -05:00
|
|
|
__ => { // Matching South or North here
|
2022-03-01 04:58:36 -06:00
|
|
|
println!("South or North");
|
|
|
|
},
|
|
|
|
_ => println!(__),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
2. ππ Match is an expression, so we can use it in assignments.
|
2022-03-01 04:58:36 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let boolean = true;
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
// Fill the blank with a match expression:
|
2022-03-01 04:58:36 -06:00
|
|
|
//
|
2022-03-01 05:44:47 -06:00
|
|
|
// boolean = true => binary = 1
|
|
|
|
// boolean = false => binary = 0
|
2022-03-01 04:58:36 -06:00
|
|
|
let binary = __;
|
|
|
|
|
|
|
|
assert_eq!(binary, 1);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
println!("Success!");
|
2022-03-01 04:58:36 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
3. ππ Using match to get the data an enum variant holds.
|
2022-03-01 04:58:36 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
// Fill in the blanks
|
2022-03-01 04:58:36 -06:00
|
|
|
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)
|
|
|
|
}
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
println!("Success!");
|
2022-03-01 04:58:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn show_message(msg: Message) {
|
|
|
|
match msg {
|
2022-03-01 06:12:55 -06:00
|
|
|
__ => { // match Message::Move
|
2022-03-01 04:58:36 -06:00
|
|
|
assert_eq!(a, 1);
|
|
|
|
assert_eq!(b, 3);
|
|
|
|
},
|
|
|
|
Message::ChangeColor(_, g, b) => {
|
|
|
|
assert_eq!(g, __);
|
|
|
|
assert_eq!(b, __);
|
|
|
|
}
|
|
|
|
__ => println!("no data in these variants")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-05-04 19:33:09 -05:00
|
|
|
### matches!
|
2022-05-04 03:32:32 -05:00
|
|
|
[`matches!`](https://doc.rust-lang.org/stable/core/macro.matches.html) looks like `match`, but can do something different.
|
2022-03-01 04:58:36 -06:00
|
|
|
|
2022-03-02 07:59:22 -06:00
|
|
|
4. ππ
|
2022-03-01 04:58:36 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
// Fill the blank with `matches!` to make the code work
|
2022-03-01 04:58:36 -06:00
|
|
|
for ab in alphabets {
|
|
|
|
assert!(__)
|
|
|
|
}
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
println!("Success!");
|
2022-03-01 04:58:36 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 07:59:22 -06:00
|
|
|
5. ππ
|
2022-03-01 04:58:36 -06:00
|
|
|
```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 {
|
2022-05-04 03:32:32 -05:00
|
|
|
if e == MyEnum::Foo { // Fix the error by changing only this line
|
2022-03-01 04:58:36 -06:00
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(count, 2);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
println!("Success!");
|
2022-03-01 04:58:36 -06:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
### If let
|
|
|
|
For some cases, when matching enums, `match` is too heavy. We can use `if let` instead.
|
2022-03-01 05:44:47 -06:00
|
|
|
|
2022-03-02 07:59:22 -06:00
|
|
|
6. π
|
2022-03-01 05:44:47 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let o = Some(7);
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
// Remove the whole `match` block, using `if let` instead
|
2022-03-01 05:44:47 -06:00
|
|
|
match o {
|
|
|
|
Some(i) => {
|
|
|
|
println!("This is a really long string and `{:?}`", i);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
println!("Success!");
|
2022-03-01 05:44:47 -06:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 07:59:22 -06:00
|
|
|
7. ππ
|
2022-03-01 05:44:47 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
// Fill in the blank
|
2022-03-01 05:44:47 -06:00
|
|
|
enum Foo {
|
|
|
|
Bar(u8)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a = Foo::Bar(1);
|
|
|
|
|
|
|
|
__ {
|
|
|
|
println!("foobar holds the value: {}", i);
|
2022-03-05 00:48:43 -06:00
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
println!("Success!");
|
2022-03-01 05:44:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-03-02 07:59:22 -06:00
|
|
|
8. ππ
|
2022-03-01 05:44:47 -06:00
|
|
|
```rust,editable
|
|
|
|
|
|
|
|
enum Foo {
|
|
|
|
Bar,
|
|
|
|
Baz,
|
|
|
|
Qux(u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a = Foo::Qux(10);
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
// Remove the codes below, using `match` instead
|
2022-03-01 05:44:47 -06:00
|
|
|
if let Foo::Bar = a {
|
2022-03-01 06:12:55 -06:00
|
|
|
println!("match foo::bar")
|
2022-03-01 05:44:47 -06:00
|
|
|
} else if let Foo::Baz = a {
|
2022-03-01 06:12:55 -06:00
|
|
|
println!("match foo::baz")
|
2022-03-01 05:44:47 -06:00
|
|
|
} else {
|
2022-03-01 06:12:55 -06:00
|
|
|
println!("match others")
|
2022-03-01 05:44:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Shadowing
|
2022-03-02 07:59:22 -06:00
|
|
|
9. ππ
|
2022-03-01 05:44:47 -06:00
|
|
|
```rust,editable
|
|
|
|
|
2022-05-04 03:32:32 -05:00
|
|
|
// Fix the errors in-place
|
2022-03-01 05:44:47 -06:00
|
|
|
fn main() {
|
|
|
|
let age = Some(30);
|
2022-05-04 03:32:32 -05:00
|
|
|
if let Some(age) = age { // Create a new variable with the same name as previous `age`
|
2022-03-01 05:44:47 -06:00
|
|
|
assert_eq!(age, Some(30));
|
2022-05-04 03:32:32 -05:00
|
|
|
} // The new variable `age` goes out of scope here
|
2022-03-01 05:44:47 -06:00
|
|
|
|
|
|
|
match age {
|
2022-05-04 03:32:32 -05:00
|
|
|
// Match can also introduce a new shadowed variable
|
2022-03-01 05:44:47 -06:00
|
|
|
Some(age) => println!("age is a new variable, it's value is {}",age),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 21:44:22 -05:00
|
|
|
```
|
2022-03-01 08:06:38 -06: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
|