Merge pull request #231 from Tanish-Eagle/editing-match

Editing match
This commit is contained in:
Sunface
2022-05-05 08:33:39 +08:00
committed by GitHub
2 changed files with 39 additions and 39 deletions

View File

@ -1,10 +1,10 @@
# match, if let # Match, if let
### match ### Match
1. 🌟🌟 1. 🌟🌟
```rust,editable ```rust,editable
// fill the blanks // Fill the blanks
enum Direction { enum Direction {
East, East,
West, West,
@ -16,7 +16,7 @@ fn main() {
let dire = Direction::South; let dire = Direction::South;
match dire { match dire {
Direction::East => println!("East"), Direction::East => println!("East"),
__ => { // matching South or North here __ => { // Matching South or North here
println!("South or North"); println!("South or North");
}, },
_ => println!(__), _ => println!(__),
@ -24,13 +24,13 @@ fn main() {
} }
``` ```
2. 🌟🌟 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() {
let boolean = true; let boolean = true;
// fill the blank with an match expression: // Fill the blank with a match expression:
// //
// boolean = true => binary = 1 // boolean = true => binary = 1
// boolean = false => binary = 0 // boolean = false => binary = 0
@ -38,14 +38,14 @@ fn main() {
assert_eq!(binary, 1); assert_eq!(binary, 1);
println!("Success!") println!("Success!");
} }
``` ```
3. 🌟🌟 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
enum Message { enum Message {
Quit, Quit,
Move { x: i32, y: i32 }, Move { x: i32, y: i32 },
@ -64,7 +64,7 @@ fn main() {
show_message(msg) show_message(msg)
} }
println!("Success!") println!("Success!");
} }
fn show_message(msg: Message) { fn show_message(msg: Message) {
@ -83,7 +83,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. 🌟🌟 4. 🌟🌟
```rust,editable ```rust,editable
@ -91,12 +91,12 @@ fn show_message(msg: Message) {
fn main() { fn main() {
let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y']; let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];
// fill the blank with `matches!` to make the code work // Fill the blank with `matches!` to make the code work
for ab in alphabets { for ab in alphabets {
assert!(__) assert!(__)
} }
println!("Success!") println!("Success!");
} }
``` ```
@ -113,19 +113,19 @@ fn main() {
let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo]; let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo];
for e in v { for e in v {
if e == MyEnum::Foo { // fix the error with changing only this line if e == MyEnum::Foo { // Fix the error by changing only this line
count += 1; count += 1;
} }
} }
assert_eq!(count, 2); assert_eq!(count, 2);
println!("Success!") println!("Success!");
} }
``` ```
### 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. 🌟 6. 🌟
```rust,editable ```rust,editable
@ -133,12 +133,12 @@ For some cases, when matching enums, `match` is too heavy, we can use `if let` i
fn main() { fn main() {
let o = Some(7); let o = Some(7);
// remove the whole `match` block, using `if let` instead // Remove the whole `match` block, using `if let` instead
match o { match o {
Some(i) => { Some(i) => {
println!("This is a really long string and `{:?}`", i); println!("This is a really long string and `{:?}`", i);
println!("Success!") println!("Success!");
} }
_ => {} _ => {}
}; };
@ -148,7 +148,7 @@ fn main() {
7. 🌟🌟 7. 🌟🌟
```rust,editable ```rust,editable
// fill in the blank // Fill in the blank
enum Foo { enum Foo {
Bar(u8) Bar(u8)
} }
@ -159,7 +159,7 @@ fn main() {
__ { __ {
println!("foobar holds the value: {}", i); println!("foobar holds the value: {}", i);
println!("Success!") println!("Success!");
} }
} }
``` ```
@ -176,7 +176,7 @@ enum Foo {
fn main() { fn main() {
let a = Foo::Qux(10); let a = Foo::Qux(10);
// remove the codes below, using `match` instead // Remove the codes below, using `match` instead
if let Foo::Bar = a { if let Foo::Bar = a {
println!("match foo::bar") println!("match foo::bar")
} else if let Foo::Baz = a { } else if let Foo::Baz = a {
@ -191,15 +191,15 @@ fn main() {
9. 🌟🌟 9. 🌟🌟
```rust,editable ```rust,editable
// fix the errors in-place // Fix the errors in-place
fn main() { fn main() {
let age = Some(30); let age = Some(30);
if let Some(age) = age { // create a new variable with the same name as previous `age` if let Some(age) = age { // Create a new variable with the same name as previous `age`
assert_eq!(age, Some(30)); assert_eq!(age, Some(30));
} // the new variable `age` goes out of scope here } // The new variable `age` goes out of scope here
match age { match age {
// match can also introduce a new shadowed variable // Match can also introduce a new shadowed variable
Some(age) => println!("age is a new variable, it's value is {}",age), Some(age) => println!("age is a new variable, it's value is {}",age),
_ => () _ => ()
} }

View File

@ -1,16 +1,16 @@
# Patterns # Patterns
1. 🌟🌟 use `|` to match several values, use `..=` to match a inclusive range 1. 🌟🌟 Use `|` to match several values, use `..=` to match an inclusive range.
```rust,editable ```rust,editable
fn main() {} fn main() {}
fn match_number(n: i32) { fn match_number(n: i32) {
match n { match n {
// match a single value // Match a single value
1 => println!("One!"), 1 => println!("One!"),
// fill in the blank with `|`, DON'T use `..` ofr `..=` // Fill in the blank with `|`, DON'T use `..` or `..=`
__ => println!("match 2 -> 5"), __ => println!("match 2 -> 5"),
// match an inclusive range // Match an inclusive range
6..=10 => { 6..=10 => {
println!("match 6 -> 10") println!("match 6 -> 10")
}, },
@ -21,7 +21,7 @@ fn match_number(n: i32) {
} }
``` ```
2. 🌟🌟🌟 The `@` operator lets us create a variable that holds a value at the same time we are testing that value to see whether it matches a pattern. 2. 🌟🌟🌟 The `@` operator lets us create a variable that holds a value, at the same time we are testing that value to see whether it matches a pattern.
```rust,editable ```rust,editable
struct Point { struct Point {
@ -30,12 +30,12 @@ struct Point {
} }
fn main() { fn main() {
// fill in the blank to let p match the second arm // Fill in the blank to let p match the second arm
let p = Point { x: __, y: __ }; let p = Point { x: __, y: __ };
match p { match p {
Point { x, y: 0 } => println!("On the x axis at {}", x), Point { x, y: 0 } => println!("On the x axis at {}", x),
// second arm // Second arm
Point { x: 0..=5, y: y@ (10 | 20 | 30) } => println!("On the y axis at {}", y), Point { x: 0..=5, y: y@ (10 | 20 | 30) } => println!("On the y axis at {}", y),
Point { x, y } => println!("On neither axis: ({}, {})", x, y), Point { x, y } => println!("On neither axis: ({}, {})", x, y),
} }
@ -46,7 +46,7 @@ fn main() {
```rust,editable ```rust,editable
// fix the errors // Fix the errors
enum Message { enum Message {
Hello { id: i32 }, Hello { id: i32 },
} }
@ -69,7 +69,7 @@ fn main() {
4. 🌟🌟 A match guard is an additional if condition specified after the pattern in a match arm that must also match, along with the pattern matching, for that arm to be chosen. 4. 🌟🌟 A match guard is an additional if condition specified after the pattern in a match arm that must also match, along with the pattern matching, for that arm to be chosen.
```rust,editable ```rust,editable
// fill in the blank to make the code work, `split` MUST be used // Fill in the blank to make the code work, `split` MUST be used
fn main() { fn main() {
let num = Some(4); let num = Some(4);
let split = 5; let split = 5;
@ -79,14 +79,14 @@ fn main() {
None => (), None => (),
} }
println!("Success!") println!("Success!");
} }
``` ```
5. 🌟🌟 Ignoring remaining parts of the value with `..` 5. 🌟🌟 Ignoring remaining parts of the value with `..`
```rust,editable ```rust,editable
// fill the blank to make the code work // Fill the blank to make the code work
fn main() { fn main() {
let numbers = (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048); let numbers = (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048);
@ -97,11 +97,11 @@ fn main() {
} }
} }
println!("Success!") println!("Success!");
} }
``` ```
6. 🌟🌟 Using pattern `&mut V` to match a mutable reference needs you to be very careful due to `V` being a value after matching 6. 🌟🌟 Using pattern `&mut V` to match a mutable reference needs you to be very careful, due to `V` being a value after matching.
```rust,editable ```rust,editable
// FIX the error with least changing // FIX the error with least changing