Rust 团队正式推出新版本的 Rust 1.55.0,如果你此前已通过 rustup 安装了以前的 Rust 版本,运行以下命令即可升级至最新版本:
rustup update stable 1.55.0 稳定版中的更新内容包括:
Cargo 重复编译器错误
在过去的版本中,当运行 cargo test、 cargo check --all-targets 或类似的命令在多个配置中构建相同的 Rust crate 时,错误和警告可能会重复出现,因为 rustc 的运行是并行的,而且都显示相同的警告。
例如,在 1.54.0 中,类似这样的输出很常见:
$ cargo +1.54.0 check --all-targets Checking foo v0.1.0 warning: function is never used: `foo` --> src/lib.rs:9:4 |9 | fn foo() {}
| ^^^
|
= note: `#[warn(dead_code)]` on by default warning: 1 warning emittedwarning: function is never used: `foo` --> src/lib.rs:9:4 |9 | fn foo() {}
| ^^^
|
= note: `#[warn(dead_code)]` on by default warning: 1 warning emitted
Finished dev [unoptimized + debuginfo] target(s) in 0.10s 在 1.55 版本中,这一行为已被调整为在编译结束后进行重复并打印报告:
$ cargo +1.55.0 check --all-targets Checking foo v0.1.0 warning: function is never used: `foo` --> src/lib.rs:9:4 |9 | fn foo() {}
| ^^^
|
= note: `#[warn(dead_code)]` on by default warning: `foo` (lib) generated 1 warning warning: `foo` (lib test) generated 1 warning (1 duplicate)
Finished dev [unoptimized + debuginfo] target(s) in 0.84s 更快、更准确的浮点数解析
标准库的浮点解析实现已经更新为使用 Eisel-Lemire 算法,这带来了速度的提高和正确性的改善。在过去,某些边缘情况无法解析,现在这已经被修复了。
std::io::ErrorKind 变体已更新
std::io::ErrorKind 是一个 #[non_exhaustive] 枚举,它将错误分为可移植的类别,如 NotFound 或 WouldBlock。拥有 std::io::Error 的 Rust 代码可以调用 kind 方法来获得 std::io::ErrorKind,并在此基础上进行匹配以处理特定错误。
并非所有的错误都被归类为 ErrorKind 值;有些错误没有被归类,而是被放置在一个万能的变体中。在以前的 Rust 版本中,未分类的错误使用 ErrorKind::Other;然而,用户创建的 std::io::Error 值也通常使用 ErrorKind::Other。在 1.55 中,未分类的错误现在使用内部变体 ErrorKind::Uncategorized;这使得 ErrorKind::Other 专门用于构造不来自标准库的 std::io::Error 值。
增加了开放范围(open range)模式
Rust 1.55 稳定了模式中开放范围的使用:
match x as u32 {
0 => println!("zero!"),
1.. => println!("positive number!"),
} 稳定的 API
以下方法和特性的实现被稳定化:
[Bound::cloned]( )[Drain::as_str]( )[IntoInnerError::into_error]( )[IntoInnerError::into_parts]( )[MaybeUninit::assume_init_mut]( )[MaybeUninit::assume_init_ref]( )[MaybeUninit::write]( )[array::map]( )[ops::ControlFlow]( )[x86::_bittest]( )[x86::_bittestandcomplement]( )[x86::_bittestandreset]( )[x86::_bittestandset]( )[x86_64::_bittest64]( )[x86_64::_bittestandcomplement64]( )[x86_64::_bittestandreset64]( )[x86_64::_bittestandset64]( )
以前稳定的函数现在是 const
[str::from_utf8_unchecked]( )
Rust 1.55.0 版本中的其他变化可查看:https://blog.rust-lang.org/2021/09/09/Rust-1.55.0.html