From 9e69201cd7a871eb4f9531009c10f69d39fa67bb Mon Sep 17 00:00:00 2001 From: Eucalypt Date: Wed, 11 Oct 2023 16:43:26 +0800 Subject: [PATCH] translation(appendix/rust-versions) : 1.73 --- src/SUMMARY.md | 1 + src/appendix/rust-versions/1.72.md | 2 +- src/appendix/rust-versions/1.73.md | 84 ++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/appendix/rust-versions/1.73.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7fa5ef01..1da57f17 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -376,3 +376,4 @@ - [1.70](appendix/rust-versions/1.70.md) - [1.71](appendix/rust-versions/1.71.md) - [1.72](appendix/rust-versions/1.72.md) + - [1.73](appendix/rust-versions/1.73.md) diff --git a/src/appendix/rust-versions/1.72.md b/src/appendix/rust-versions/1.72.md index 720f0f8c..b345b56c 100644 --- a/src/appendix/rust-versions/1.72.md +++ b/src/appendix/rust-versions/1.72.md @@ -41,7 +41,7 @@ error: could not compile `my-project` (bin "my-project") due to previous error 几个原本由 Clippy 提供的 lints,提升到 rustc 里: -* [`clippy::undropped_manually_drops`](https://rust-lang.github.io/rust-clippy/rust-1.71.0/index.html#undropped_manually_drops) => [undropped-manually-drops](https://doc.rust-lang.org/1.72.0/rustc/lints/listing/deny-by-default.html#undropped-manually-drops) (deny) +* [`clippy::undropped_manually_drops`](https://rust-lang.github.io/rust-clippy/rust-1.71.0/index.html#undropped_manually_drops) => [`undropped-manually-drops`](https://doc.rust-lang.org/1.72.0/rustc/lints/listing/deny-by-default.html#undropped-manually-drops) (deny) * 无作用的 `ManullyDrop` * [`clippy::invalid_utf8_in_unchecked`](https://rust-lang.github.io/rust-clippy/rust-1.71.0/index.html#invalid_utf8_in_unchecked) => [`invalid_from_utf8_unchecked`](https://doc.rust-lang.org/1.72.0/rustc/lints/listing/deny-by-default.html#invalid-from-utf8-unchecked) (deny) 或 [`invalid_from_utf8`](https://doc.rust-lang.org/1.72.0/rustc/lints/listing/warn-by-default.html#invalid-from-utf8) (warn) * 检查调用 `std::str::from_utf8_unchecked` 和 `std::str::from_utf8_unchecked_mut` 转换不合法的 UTF-8 字面量,这会违反了 safety 前提,导致未定义行为 diff --git a/src/appendix/rust-versions/1.73.md b/src/appendix/rust-versions/1.73.md new file mode 100644 index 00000000..f89f018a --- /dev/null +++ b/src/appendix/rust-versions/1.73.md @@ -0,0 +1,84 @@ +# Rust 新版解读 | 1.73 | panic 报错展示优化 + +> Rust 1.73 官方 release doc: [Announcing Rust 1.73.0 | Rust Blog](https://blog.rust-lang.org/2023/10/05/Rust-1.73.0.html) + +通过 [rustup](https://www.rust-lang.org/tools/install) 安装的同学可以使用以下命令升级到 1.73 版本: + +```shell +$ rustup update stable +``` + +## 更简洁的 panic 报错信息 + +默认的 panic handler 会把报错信息单独列出一行,当报错信息很长、包含多行或者嵌套结构的时候可读性会更强。 + +```rust +fn main() { + let file = "ferris.txt"; + panic!("oh no! {file:?} not found!"); +} +``` + +Rust 1.73 之前的: +```shell +thread 'main' panicked at 'oh no! "ferris.txt" not found!', src/main.rs:3:5 +``` + +Rust 1.73 之后的: +```shell +thread 'main' panicked at src/main.rs:3:5: +oh no! "ferris.txt" not found! +``` + +另外,由 `assert_eq` 和 `assert_ne` 产生的 panic 消息也把自定义信息部分(第三个参数)的展示位置改动了一下: + +```rust +fn main() { + assert_eq!("🦀", "🐟", "ferris is not a fish"); +} +``` + +Rust 1.73 之前的: +```shell +thread 'main' panicked at 'assertion failed: `(left == right)` + left: `"🦀"`, +right: `"🐟"`: ferris is not a fish', src/main.rs:2:5 +``` + +Rust 1.73 之后的: +```shell +thread 'main' panicked at src/main.rs:2:5: +assertion `left == right` failed: ferris is not a fish + left: "🦀" +right: "🐟" +``` + +## 线程局部初始化 + +如 [RFC 3184](https://github.com/rust-lang/rfcs/blob/master/text/3184-thread-local-cell-methods.md) 提案, `LocalKey>` 和 `LocalKey>` 现在可以直接用 `get()`, `set()`, `take()` 和 `replace()` 方法来操作,不再需要写 `with(|inner|...)` 的闭包形式。声明线程静态局部变量的宏 `thread_local!` 内部就是使用的就是 `LocalKey`。 + +新的方法让代码更简洁,也避免了默认值在新线程运行额外的初始化代码。 + +```rust +thread_local! { + static THINGS: Cell> = Cell::new(Vec::new()); +} + +fn f() { + // before: + THINGS.with(|i| i.set(vec![1, 2, 3])); + // now: + THINGS.set(vec![1, 2, 3]); + + // ... + + // before: + let v = THINGS.with(|i| i.take()); + // now: + let v: Vec = THINGS.take(); +} +``` + +## Others + +其它更新细节,和稳定的API列表,参考[原Blog](https://blog.rust-lang.org/2023/10/05/Rust-1.73.0.html#stabilized-apis)