From c5be42b9e8ad40fb42869120510163ea0e2ba944 Mon Sep 17 00:00:00 2001 From: sunface Date: Thu, 13 Jan 2022 17:03:16 +0800 Subject: [PATCH] =?UTF-8?q?update=E5=85=A8=E6=A8=A1=E5=BC=8F=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../concurrency-with-threads/send-sync.md | 2 +- .../basic/match-pattern/all-patterns.md | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/book/contents/advance/concurrency-with-threads/send-sync.md b/book/contents/advance/concurrency-with-threads/send-sync.md index a225d169..d83ae4cf 100644 --- a/book/contents/advance/concurrency-with-threads/send-sync.md +++ b/book/contents/advance/concurrency-with-threads/send-sync.md @@ -91,7 +91,7 @@ fn main() { t.join().unwrap(); } ``` -报错跟之前无二: `*mut u8 cannot be sent between threads safely`, 但是有一个问题,我们无法为其直接实现`Send`特征,好在可以用[`newtype`类型](../custom-type.md#newtype):`struct MyBox(*mut u8);`。 +报错跟之前无二: `*mut u8 cannot be sent between threads safely`, 但是有一个问题,我们无法为其直接实现`Send`特征,好在可以用[`newtype`类型](../custom-type.md#newtype) :`struct MyBox(*mut u8);`。 还记得之前的规则吗:复合类型中有一个成员没实现`Send`,该复合类型就不是`Send`,因此我们需要手动为它实现: ```rust diff --git a/book/contents/basic/match-pattern/all-patterns.md b/book/contents/basic/match-pattern/all-patterns.md index c9f6a2df..e7d2a3a0 100644 --- a/book/contents/basic/match-pattern/all-patterns.md +++ b/book/contents/basic/match-pattern/all-patterns.md @@ -561,6 +561,31 @@ match msg { 当你既想要限定分支范围,又想要使用分支的变量时,就可以用`@`来绑定到一个新的变量上,实现想要的功能。 +#### @前绑定后解构(Rust1.56新增) +使用`@`还可以在绑定新变量的同时,对目标进行解构: +```rust +#[derive(Debug)] +struct Point { + x: i32, + y: i32, +} + +fn main() { + // 绑定新变量`p`,同时对`Point`进行解构 + let p @ Point {x: px, y: py } = Point {x: 10, y: 23}; + println!("x: {}, y: {}", px, py); + println!("{:?}", p); + + + let point = Point {x: 10, y: 5}; + if let p @ Point {x: 10, y} = point { + println!("x is 10 and y is {} in {:?}", y, p); + } else { + println!("x was not 10 :("); + } +} +``` + #### @新特性(Rust1.53新增) 考虑下面一段代码: ```rust