From 5807a3147d2d15439ed1ce509cca9360738504f6 Mon Sep 17 00:00:00 2001 From: sunface Date: Wed, 2 Mar 2022 21:59:22 +0800 Subject: [PATCH] add solutions for match.md --- solutions/pattern-match/match.md | 168 +++++++++++++++++++++++++++++++ src/pattern-match/match-iflet.md | 18 ++-- 2 files changed, 177 insertions(+), 9 deletions(-) diff --git a/solutions/pattern-match/match.md b/solutions/pattern-match/match.md index e69de29..8910145 100644 --- a/solutions/pattern-match/match.md +++ b/solutions/pattern-match/match.md @@ -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), + _ => () + } + } +``` \ No newline at end of file diff --git a/src/pattern-match/match-iflet.md b/src/pattern-match/match-iflet.md index a4dc682..2ed54eb 100644 --- a/src/pattern-match/match-iflet.md +++ b/src/pattern-match/match-iflet.md @@ -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