Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translation(appendix/rust-versions) : 1.73 #1305

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/appendix/rust-versions/1.72.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 前提,导致未定义行为
Expand Down
84 changes: 84 additions & 0 deletions src/appendix/rust-versions/1.73.md
Original file line number Diff line number Diff line change
@@ -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<Cell<T>>` 和 `LocalKey<RefCell<T>>` 现在可以直接用 `get()`, `set()`, `take()` 和 `replace()` 方法来操作,不再需要写 `with(|inner|...)` 的闭包形式。声明线程静态局部变量的宏 `thread_local!` 内部就是使用的就是 `LocalKey<T>`。

新的方法让代码更简洁,也避免了默认值在新线程运行额外的初始化代码。

```rust
thread_local! {
static THINGS: Cell<Vec<i32>> = 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<i32> = THINGS.take();
}
```

## Others

其它更新细节,和稳定的API列表,参考[原Blog](https://blog.rust-lang.org/2023/10/05/Rust-1.73.0.html#stabilized-apis)