From 553f7b1648db1e4399dcd4356ec4a7b41edd7628 Mon Sep 17 00:00:00 2001 From: sunface Date: Fri, 11 Mar 2022 15:45:03 +0800 Subject: [PATCH] add one exercise in patterns.md --- solutions/pattern-match/patterns.md | 13 +++++++++++++ src/pattern-match/patterns.md | 15 +++++++++++++++ zh-CN/src/pattern-match/patterns.md | 15 +++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/solutions/pattern-match/patterns.md b/solutions/pattern-match/patterns.md index 17149b8..18b9ee3 100644 --- a/solutions/pattern-match/patterns.md +++ b/solutions/pattern-match/patterns.md @@ -85,4 +85,17 @@ fn main() { } } } +``` + +6. +```rust +fn main() { + let mut v = String::from("hello,"); + let r = &mut v; + + match r { + // The type of value is &mut String + value => value.push_str(" world!") + } +} ``` \ No newline at end of file diff --git a/src/pattern-match/patterns.md b/src/pattern-match/patterns.md index d438705..cddc1e2 100644 --- a/src/pattern-match/patterns.md +++ b/src/pattern-match/patterns.md @@ -101,4 +101,19 @@ fn main() { } ``` +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 + +// FIX the error with least changing +// DON'T remove any code line +fn main() { + let mut v = String::from("hello,"); + let r = &mut v; + + match r { + &mut value => value.push_str(" world!") + } +} +```` + > You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it \ No newline at end of file diff --git a/zh-CN/src/pattern-match/patterns.md b/zh-CN/src/pattern-match/patterns.md index d45c688..e9ef131 100644 --- a/zh-CN/src/pattern-match/patterns.md +++ b/zh-CN/src/pattern-match/patterns.md @@ -97,4 +97,19 @@ fn main() { } ``` +6. 🌟🌟 使用模式 `&mut V` 去匹配一个可变引用时,你需要格外小心,因为匹配出来的 `V` 是一个值,而不是可变引用 +```rust,editable + +// 修复错误,尽量少地修改代码 +// 不要移除任何代码行 +fn main() { + let mut v = String::from("hello,"); + let r = &mut v; + + match r { + &mut value => value.push_str(" world!") + } +} +```` + > 你可以在[这里](https://github.com/sunface/rust-by-practice)找到答案(在 solutions 路径下) \ No newline at end of file