2024 年 7 月 25 日 · Rust 发布团队
Rust 团队很高兴宣布 Rust 的新版本 1.80.0。Rust 是一种编程语言,可帮助每个人构建可靠、高效的软件。
如果您已通过安装了旧版本的 Rust rustup,则可以通过以下方式获取 1.80.0:
$ rustup update stable
如果您还没有安装 rustup,您可以从我们网站的相应页面获取,并查看 1.80.0 的详细发行说明。
如果您想通过测试未来版本来帮助我们,您可以考虑在本地更新以使用测试频道 ( rustup default beta) 或夜间频道 ( rustup default nightly)。请报告您可能遇到的任何错误!
1.80.0 稳定版包含哪些内容
LazyCell和LazyLock
这些“惰性”类型会延迟初始化其数据,直到首次访问。它们类似于 1.70 中稳定的 OnceCell 和 OnceLock 类型,但初始化函数包含在单元格中。这完成了从流行的 lazy_static 和 once_cell crate 中采用到标准库中的功能的稳定化。
LazyLock 是线程安全的选项,因此适合用于 static 值之类的地方。例如,线程 spawn 和主函数 scope 都将看到完全相同的持续时间,因为 LAZY_TIME 将由最终首先访问静态函数的函数初始化一次。与 OnceLock::get_or_init() 不同,这两种用法都不必知道如何初始化它。
use std::sync::LazyLock;
use std::time::Instant;
static LAZY_TIME: LazyLock<Instant> = LazyLock::new(Instant::now);
fn main() {
let start = Instant::now();
std::thread::scope(|s| {
s.spawn(|| {
println!("Thread lazy time is {:?}", LAZY_TIME.duration_since(start));
});
println!("Main lazy time is {:?}", LAZY_TIME.duration_since(start));
});
}
LazyCell 在没有线程同步的情况下执行相同的操作,因此它没有实现 Sync,而 static 是所需的,但它仍然可以在 thread_local! 静态中使用(每个线程都有不同的初始化)。根据线程安全需求,任一类型也可以在其他数据结构中使用,因此延迟初始化在任何地方都可用!
检查 cfg 名称和值
在 1.79 中,rustc 稳定了一个 --check-cfg 标志,现在 Cargo 1.80 正在对其 cfg 知道的所有名称和值启用这些检查(除了来自 rustc 的众所周知的名称和值 之外)。这包括来自 Cargo.toml 的功能名称以及 cargo::rustc-check-cfg 来自构建脚本的新输出。
默认警告 unexpected_cfgs lint 会报告意外的 cfg,用于捕获拼写错误或其他错误配置。例如,在具有可选 rayon 依赖项的项目中,此代码配置了错误的 feature 值:
fn main() {
println!("Hello, world!");
#[cfg(feature = "crayon")]
rayon::join(
|| println!("Hello, Thing One!"),
|| println!("Hello, Thing Two!"),
);
}
warning: unexpected `cfg` condition value: `crayon`
--> src/main.rs:4:11
|
4 | #[cfg(feature = "crayon")]
| ^^^^^^^^^^--------
| |
| help: there is a expected value with a similar name: `"rayon"`
|
= note: expected values for `feature` are: `rayon`
= help: consider adding `crayon` as a feature in `Cargo.toml`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default
无论实际 rayon 功能是否启用,都会报告相同的警告。
[lints] 清单中的表格还可 Cargo.toml 用于扩展自定义的已知名称和值的列表 cfg。 rustc 自动提供警告中使用的 语法。
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(foo, values("bar"))'] }
图案的独特范围
Rust 范围模式现在可以使用独占端点,写成 a..b 或 ..b 类似于 Range 和 RangeTo 表达式类型。例如,以下模式现在可以对一个模式的结束和下一个模式的开始使用相同的常量:
pub fn size_prefix(n: u32) -> &'static str {
const K: u32 = 10u32.pow(3);
const M: u32 = 10u32.pow(6);
const G: u32 = 10u32.pow(9);
match n {
..K => "",
K..M => "k",
M..G => "M",
G.. => "G",
}
}
以前,模式中只允许包含(a..=b 或 ..=b)或开放(a..)范围,因此像这样的代码需要为包含端点(如 K - 1)设置单独的常量。
长期以来,排他范围一直作为一项不稳定的功能实施,但阻碍因素在于它们可能会增加混乱并增加模式中出现差一错误的几率。为此,我们增强了详尽性检查,以更好地检测模式匹配中的差距,新的 lint non_contiguous_range_endpoints 和 overlapping_range_endpoints 将有助于检测您可能需要将排他模式切换为包含模式或反之亦然的情况。
稳定的 API
impl Default for Rc<CStr>impl Default for Rc<str>impl Default for Rc<[T]>impl Default for Arc<str>impl Default for Arc<CStr>impl Default for Arc<[T]>impl IntoIterator for Box<[T]>impl FromIterator<String> for Box<str>impl FromIterator<char> for Box<str>LazyCellLazyLockDuration::div_duration_f32Duration::div_duration_f64Option::take_ifSeek::seek_relativeBinaryHeap::as_sliceNonNull::offsetNonNull::byte_offsetNonNull::addNonNull::byte_addNonNull::subNonNull::byte_subNonNull::offset_fromNonNull::byte_offset_fromNonNull::readNonNull::read_volatileNonNull::read_unalignedNonNull::writeNonNull::write_volatileNonNull::write_unalignedNonNull::write_bytesNonNull::copy_toNonNull::copy_to_nonoverlappingNonNull::copy_fromNonNull::copy_from_nonoverlappingNonNull::replaceNonNull::swapNonNull::drop_in_placeNonNull::align_offset<[T]>::split_at_checked<[T]>::split_at_mut_checkedstr::split_at_checkedstr::split_at_mut_checkedstr::trim_asciistr::trim_ascii_startstr::trim_ascii_end<[u8]>::trim_ascii<[u8]>::trim_ascii_start<[u8]>::trim_ascii_endIpv4Addr::BITSIpv4Addr::to_bitsIpv4Addr::from_bitsIpv6Addr::BITSIpv6Addr::to_bitsIpv6Addr::from_bitsVec::<[T; N]>::into_flattened<[[T; N]]>::as_flattened<[[T; N]]>::as_flattened_mut
这些 API 现在在 const 上下文中是稳定的:
其他变化
检查 Rust、Cargo 和 Clippy 中发生的所有变化。
1.80.0 的贡献者
许多人齐心协力创建了 Rust 1.80.0。如果没有你们所有人,我们就无法做到这一点。谢谢!