You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 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)