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

210 lines
3.7 KiB
Markdown
Raw Normal View History

# match, if let
2022-03-01 04:58:36 -06:00
### match
2022-03-02 07:59:22 -06:00
1. 🌟🌟
2022-03-01 04:58:36 -06:00
```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!(__),
};
}
```
2022-03-02 07:59:22 -06: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;
// fill the blank with an match expression:
//
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);
println!("Success!")
2022-03-01 04:58:36 -06:00
}
```
2022-03-02 07:59:22 -06:00
3. 🌟🌟 using match to get the data an enum variant holds
2022-03-01 04:58:36 -06:00
```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!")
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")
}
}
```
### matches!
[`matches!`](https://doc.rust-lang.org/stable/core/macro.matches.html) looks like `match`, but can do something different
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'];
// fill the blank with `matches!` to make the code work
for ab in alphabets {
assert!(__)
}
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 {
if e == MyEnum::Foo { // fix the error with changing only this line
count += 1;
}
}
assert_eq!(count, 2);
println!("Success!")
2022-03-01 04:58:36 -06:00
}
```
2022-03-01 05:44:47 -06:00
### if let
For some cases, when matching enums, `match` is too heavy, we can use `if let` instead.
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);
// remove the whole `match` block, using `if let` instead
match o {
Some(i) => {
println!("This is a really long string and `{:?}`", i);
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
// fill in the blank
enum Foo {
Bar(u8)
}
fn main() {
let a = Foo::Bar(1);
__ {
println!("foobar holds the value: {}", i);
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);
// remove the codes below, using `match` instead
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
// 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),
_ => ()
}
}
```
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