add solutions for match.md
This commit is contained in:
parent
8019f09714
commit
5807a3147d
|
@ -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
|
||||
ππ
|
||||
1. ππ
|
||||
```rust,editable
|
||||
|
||||
// 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
|
||||
|
||||
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
|
||||
|
||||
// fill in the blanks
|
||||
|
@ -81,7 +81,7 @@ fn show_message(msg: Message) {
|
|||
### 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() {
|
||||
|
@ -94,7 +94,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
ππ
|
||||
5. ππ
|
||||
```rust,editable
|
||||
|
||||
enum MyEnum {
|
||||
|
@ -119,7 +119,7 @@ fn main() {
|
|||
### if let
|
||||
For some cases, when matching enums, `match` is too heavy, we can use `if let` instead.
|
||||
|
||||
π
|
||||
6. π
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
|
@ -135,7 +135,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
ππ
|
||||
7. ππ
|
||||
```rust,editable
|
||||
|
||||
// fill in the blank
|
||||
|
@ -152,7 +152,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
ππ
|
||||
8. ππ
|
||||
```rust,editable
|
||||
|
||||
enum Foo {
|
||||
|
@ -176,7 +176,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### Shadowing
|
||||
ππ
|
||||
9. ππ
|
||||
```rust,editable
|
||||
|
||||
// fix the errors in-place
|
||||
|
|
Loadingβ¦
Reference in New Issue