IT资讯 Rust 1.73.0 发布

thaddeus · 2023-10-10 21:00:07 · 热度: 7

Rust 1.73.0 稳定版已正式发布,主要带来以下变化:

Cleaner panic messages

默认紧急处理程序生成的输出已更改为将 panic 消息放在单独一行,而不是用引号括起来。这可以使 panic 消息更易于阅读,如本示例所示:

fn main() {
    let file = "ferris.txt";
    panic!("oh no! {file:?} not found!");
}

Output before Rust 1.73:

thread 'main' panicked at 'oh no! "ferris.txt" not found!', src/main.rs:3:5

Output starting in Rust 1.73:

thread 'main' panicked at src/main.rs:3:5:
oh no! "ferris.txt" not found!

这在信息较长、包含嵌套引号或跨多行时尤其有用。

此外,assert_eqassert_ne产生的 panic 消息也已被修改,移动了自定义消息(第三个参数)并删除了一些不必要的标点符号,如下所示:

fn main() {
    assert_eq!("🦀", "🐟", "ferris is not a fish");
}

Output before Rust 1.73:

thread 'main' panicked at 'assertion failed: `(left == right)`
 left: `"🦀"`,
right: `"🐟"`: ferris is not a fish', src/main.rs:2:5

Output starting in Rust 1.73:

thread 'main' panicked at src/main.rs:2:5:
assertion `left == right` failed: ferris is not a fish
 left: "🦀"
right: "🐟"

Thread local initialization

正如 RFC 3184 中所提议的,LocalKey<Cell<T>>LocalKey<RefCell<T>>现在可以直接使用get()set()take()replace()方法进行操作,而无需像一般LocalKey工作那样跳过with(|inner| ...)闭包。LocalKey<T>thread_local!statics 的类型。

新方法使 common code 更加简洁,并避免了为新线程在thread_local!中指定的默认值运行额外的初始化代码。

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();
}

Stabilized APIs

这些 API 现在在 const contexts 中是稳定的:

 详情可查看官方公告

为您推荐与 rust 相关的帖子:

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册