mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 20:49:41 +00:00
add solutions for match.md
This commit is contained in:
@ -0,0 +1,168 @@
|
|||||||
|
1.
|
||||||
|
```rust
|
||||||
|
enum Direction {
|
||||||
|
East,
|
||||||
|
West,
|
||||||
|
North,
|
||||||
|
South,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let dire = Direction::South;
|
||||||
|
match dire {
|
||||||
|
Direction::East => println!("East"),
|
||||||
|
Direction::South | Direction::North => { // matching South or North here
|
||||||
|
println!("South or North");
|
||||||
|
},
|
||||||
|
_ => println!("West"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
2.
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let boolean = true;
|
||||||
|
|
||||||
|
// fill the blank with an match expression:
|
||||||
|
//
|
||||||
|
// boolean = true => binary = 1
|
||||||
|
// boolean = false => binary = 0
|
||||||
|
let binary = match boolean {
|
||||||
|
true => 1,
|
||||||
|
false => 0
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(binary, 1);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
3.
|
||||||
|
```rust
|
||||||
|
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 {
|
||||||
|
Message::Move{x: a, y: b} => { // match Message::Move
|
||||||
|
assert_eq!(a, 1);
|
||||||
|
assert_eq!(b, 3);
|
||||||
|
},
|
||||||
|
Message::ChangeColor(_, g, b) => {
|
||||||
|
assert_eq!(g, 255);
|
||||||
|
assert_eq!(b, 0);
|
||||||
|
}
|
||||||
|
__ => println!("no data in these variants")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
4.
|
||||||
|
```rust
|
||||||
|
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!(matches!(ab, 'a'..='z' | 'A'..='Z' | '0' | '9'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
5.
|
||||||
|
```rust
|
||||||
|
enum MyEnum {
|
||||||
|
Foo,
|
||||||
|
Bar
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo];
|
||||||
|
for e in v {
|
||||||
|
if matches!(e , MyEnum::Foo) { // fix the error with changing only this line
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(count, 2);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
6.
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let o = Some(7);
|
||||||
|
|
||||||
|
if let Some(i) = o {
|
||||||
|
println!("This is a really long string and `{:?}`", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
7.
|
||||||
|
```rust
|
||||||
|
enum Foo {
|
||||||
|
Bar(u8)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = Foo::Bar(1);
|
||||||
|
|
||||||
|
if let Foo::Bar(i) = a {
|
||||||
|
println!("foobar holds the value: {}", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
8.
|
||||||
|
```rust
|
||||||
|
enum Foo {
|
||||||
|
Bar,
|
||||||
|
Baz,
|
||||||
|
Qux(u32)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = Foo::Qux(10);
|
||||||
|
|
||||||
|
match a {
|
||||||
|
Foo::Bar => println!("match foo::bar"),
|
||||||
|
Foo::Baz => println!("match foo::baz"),
|
||||||
|
_ => println!("match others")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
9.
|
||||||
|
```rust
|
||||||
|
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, 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),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
@ -1,7 +1,7 @@
|
|||||||
# match, if let
|
# match, if let
|
||||||
|
|
||||||
### match
|
### match
|
||||||
🌟🌟
|
1. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill the blanks
|
// fill the blanks
|
||||||
@ -24,7 +24,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟 match is an expression, so we can use it in assignments
|
2. 🌟🌟 match is an expression, so we can use it in assignments
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -40,7 +40,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟 using match to get the data an enum variant holds
|
3. 🌟🌟 using match to get the data an enum variant holds
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the blanks
|
// fill in the blanks
|
||||||
@ -81,7 +81,7 @@ fn show_message(msg: Message) {
|
|||||||
### matches!
|
### matches!
|
||||||
[`matches!`](https://doc.rust-lang.org/stable/core/macro.matches.html) looks like `match`, but can do something different
|
[`matches!`](https://doc.rust-lang.org/stable/core/macro.matches.html) looks like `match`, but can do something different
|
||||||
|
|
||||||
🌟🌟
|
4. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -94,7 +94,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟
|
5. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
enum MyEnum {
|
enum MyEnum {
|
||||||
@ -119,7 +119,7 @@ fn main() {
|
|||||||
### if let
|
### if let
|
||||||
For some cases, when matching enums, `match` is too heavy, we can use `if let` instead.
|
For some cases, when matching enums, `match` is too heavy, we can use `if let` instead.
|
||||||
|
|
||||||
🌟
|
6. 🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -135,7 +135,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟
|
7. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fill in the blank
|
// fill in the blank
|
||||||
@ -152,7 +152,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
🌟🌟
|
8. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
enum Foo {
|
enum Foo {
|
||||||
@ -176,7 +176,7 @@ fn main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Shadowing
|
### Shadowing
|
||||||
🌟🌟
|
9. 🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
// fix the errors in-place
|
// fix the errors in-place
|
||||||
|
Reference in New Issue
Block a user