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_eq
和assert_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
- Unsigned
{integer}::div_ceil
- Unsigned
{integer}::next_multiple_of
- Unsigned
{integer}::checked_next_multiple_of
std::ffi::FromBytesUntilNulError
std::os::unix::fs::chown
std::os::unix::fs::fchown
std::os::unix::fs::lchown
LocalKey::<Cell<T>>::get
LocalKey::<Cell<T>>::set
LocalKey::<Cell<T>>::take
LocalKey::<Cell<T>>::replace
LocalKey::<RefCell<T>>::with_borrow
LocalKey::<RefCell<T>>::with_borrow_mut
LocalKey::<RefCell<T>>::set
LocalKey::<RefCell<T>>::take
LocalKey::<RefCell<T>>::replace
这些 API 现在在 const contexts 中是稳定的:
rc::Weak::new
sync::Weak::new
NonNull::as_ref
详情可查看官方公告。