diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 3a51f65..9ccac30 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -19,7 +19,7 @@ - [Enum](compound-types/enum.md) - [Flow Control](flow-control.md) - [Pattern Match todo](pattern-match/intro.md) - - [match, if let](pattern-match/match-iflet.md) + - [match, matches! and if let](pattern-match/match-iflet.md) - [Option destructing](pattern-match/option.md) - [Patterns](pattern-match/patterns.md) - [Method todo](method.md) diff --git a/src/compound-types/enum.md b/src/compound-types/enum.md index 80e0cac..ad916d3 100644 --- a/src/compound-types/enum.md +++ b/src/compound-types/enum.md @@ -48,7 +48,7 @@ fn main() { } ``` -🌟🌟🌟 we can get the data which a enum variant is holding by pattern match +🌟🌟 we can get the data which a enum variant is holding by pattern match ```rust,editable // 仅填空,不要修改其它代码! @@ -63,14 +63,14 @@ fn main() { let msg = Message::Move{x: 1, y: 1}; if let Message::Move{__} = msg { - assert_eq!(x, y); + assert_eq!(a, b); + } else { + panic!("NEVER LET THIS RUN!"); } - - panic!("NEVER LET THIS RUN!"); } ``` -🌟🌟🌟 使用枚举对类型进行同一化 +🌟🌟 使用枚举对类型进行同一化 ```rust,editable @@ -102,7 +102,8 @@ fn show_message(msg: Message) { 🌟🌟 As there is no `null` in Rust, we have to use enum `Option` to deal the cases when value is absent. ```rust,editable -// Only fill in the blanks, DON'T change other code! +// fill in the blank to make the `println` work. +// also add some code to prevent the `panic` from running. fn main() { let five = Some(5); let six = plus_one(five); @@ -110,8 +111,8 @@ fn main() { if let __ = six { println!("{}", n) - } - + } + panic!("NEVER LET THIS RUN!"); } diff --git a/src/functional-programing/iterator.md b/src/functional-programing/iterator.md index 68ae1d7..641413e 100644 --- a/src/functional-programing/iterator.md +++ b/src/functional-programing/iterator.md @@ -1,5 +1,7 @@ # Iterator +https://doc.rust-lang.org/stable/rust-by-example/flow_control/for.html#for-and-iterators + ```rust,editable // (all the type annotations are superfluous) // A reference to a string allocated in read only memory diff --git a/src/pattern-match/intro.md b/src/pattern-match/intro.md index ee4bb18..17703ae 100644 --- a/src/pattern-match/intro.md +++ b/src/pattern-match/intro.md @@ -1 +1,6 @@ # Pattern Match +Learning resources: +- English: [Rust Book 18](https://doc.rust-lang.org/book/ch18-00-patterns.html) +- 简体中文: [Rust语言圣经 - 模式匹配](https://course.rs/basic/match-pattern/intro.html) + + diff --git a/src/pattern-match/match-iflet.md b/src/pattern-match/match-iflet.md index 18eed0e..82426db 100644 --- a/src/pattern-match/match-iflet.md +++ b/src/pattern-match/match-iflet.md @@ -1 +1,119 @@ # match, if let + +### match +🌟🌟 +```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!(__), + }; +} +``` + +🌟🌟 match is an expression, so we can use it in assignments +```rust,editable + +fn main() { + let boolean = true; + + // fill the blank with an match expression: + // + // boolean = true, binary = 1 + // boolean = false, binary = 0 + let binary = __; + + assert_eq!(binary, 1); +} +``` + +🌟🌟 using match to get the data an enum variant holds +```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) + } +} + +fn show_message(msg: Message) { + match msg { + __ => { // matches Message::Move + 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 + +🌟🌟 +```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!(__) + } +} +``` + +🌟🌟 +```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); +} +``` + +### if let \ No newline at end of file diff --git a/zh-CN/src/SUMMARY.md b/zh-CN/src/SUMMARY.md index 2e83f3d..6ecda0f 100644 --- a/zh-CN/src/SUMMARY.md +++ b/zh-CN/src/SUMMARY.md @@ -19,7 +19,7 @@ - [枚举](compound-types/enum.md) - [流程控制](flow-control.md) - [模式匹配 todo](pattern-match/intro.md) - - [match 和 if let](pattern-match/match-iflet.md) + - [match, matches! 和 if let](pattern-match/match-iflet.md) - [解构 Option](pattern-match/option.md) - [模式](pattern-match/patterns.md) - [方法 Method todo](method.md) diff --git a/zh-CN/src/compound-types/enum.md b/zh-CN/src/compound-types/enum.md index 4b0c76c..42ac114 100644 --- a/zh-CN/src/compound-types/enum.md +++ b/zh-CN/src/compound-types/enum.md @@ -48,7 +48,7 @@ fn main() { } ``` -🌟🌟🌟 枚举成员中的值可以使用模式匹配来获取 +🌟🌟 枚举成员中的值可以使用模式匹配来获取 ```rust,editable // 仅填空,不要修改其它代码! @@ -63,14 +63,14 @@ fn main() { let msg = Message::Move{x: 1, y: 1}; if let Message::Move{__} = msg { - assert_eq!(x, y); + assert_eq!(a, b); + } else { + panic!("不要让这行代码运行!"); } - - panic!("不要让这行代码运行!"); } ``` -🌟🌟🌟 使用枚举对类型进行同一化 +🌟🌟 使用枚举对类型进行同一化 ```rust,editable @@ -102,7 +102,7 @@ fn show_message(msg: Message) { 🌟🌟 Rust 中没有 `null`,我们通过 `Option` 枚举来处理值为空的情况 ```rust,editable -// 仅填空,不要修改其它代码! +// 填空让 `println` 输出,同时添加一些代码不要让最后一行的 `panic` 执行到 fn main() { let five = Some(5); let six = plus_one(five); @@ -110,8 +110,8 @@ fn main() { if let __ = six { println!("{}", n) - } - + } + panic!("不要让这行代码运行!"); } diff --git a/zh-CN/src/functional-programing/iterator.md b/zh-CN/src/functional-programing/iterator.md index 21ce83e..4fb4459 100644 --- a/zh-CN/src/functional-programing/iterator.md +++ b/zh-CN/src/functional-programing/iterator.md @@ -1,3 +1,2 @@ # Iterator -https://doc.rust-lang.org/stable/rust-by-example/flow_control/for.html#for-and-iterators \ No newline at end of file