Rust 1.95.0:cfg_select!、match 中的 if let guard,以及一批 const 稳定化
Rust 1.95.0 于 2026-04-16 发布,rustup update stable 即可升级。官方公告见 Announcing Rust 1.95.0,完整 release notes 见 releases.html。
cfg_select!
1.95 引入了 cfg_select! 宏,相当于编译期的 cfg match:它展开为第一个配置谓词为 true 的分支的右值。功能上可以替代 cfg-if crate,但语法不同。
cfg_select! {
unix => {
fn foo() { /* unix specific functionality */ }
}
target_pointer_width = "32" => {
fn foo() { /* non-unix, 32-bit functionality */ }
}
_ => {
fn foo() { /* fallback implementation */ }
}
}
let is_windows_str = cfg_select! {
windows => "windows",
_ => "not windows",
};
match 里的 if let guard
Rust 1.88 稳定了 let chains,1.95 把这一能力带进 match 表达式,允许守卫子句基于模式匹配做条件判断:
match value {
Some(x) if let Ok(y) = compute(x) => {
// Both `x` and `y` are available here
println!("{}, {}", x, y);
}
_ => {}
}
需要注意:编译器当前不会把 if let guard 中匹配的模式纳入整个 match 的穷尽性分析,这一点与 if guard 一致。
稳定化的 API
MaybeUninit: FromMaybeUninit: AsRef/AsRefMaybeUninit: AsMut/AsMut[MaybeUninit; N]: From>Cell: AsRef;Cell: AsRef;Cell: AsRefbool: TryFromAtomicPtr::update/try_updateAtomicBool::update/try_updateAtomicI8/I16/I32/I64/Isize的update/try_update(各整数原子类型同理)cfg_select!mod core::range;core::range::RangeInclusive;core::range::RangeInclusiveItercore::hint::cold_path::as_ref_unchecked;::as_ref_unchecked;::as_mut_uncheckedVec::push_mut;Vec::insert_mutVecDeque::push_front_mut/push_back_mut/insert_mutLinkedList::push_front_mut/push_back_mutLayout::dangling_ptr;Layout::repeat;Layout::repeat_packed;Layout::extend_packed
以下 API 此前已稳定,现在在 const 上下文中也可用:
fmt::from_fnControlFlow::is_breakControlFlow::is_continue
语言
- 稳定
match分支上的if letguards irrefutable_let_patternslint 不再对 let chains 报警- 支持重命名导入 path-segment 关键字
- 稳定 PowerPC / PowerPC64 的内联汇编
- const-eval:类型化拷贝时 padding 行为更一致
- const 块不再被求值用于判断涉及 fallible 操作的表达式能否隐式常量提升
- 让模式匹配的操作语义与 crate、模块无关
编译器
- 稳定
--remap-path-scope,控制路径在生成二进制中的重映射作用域 - 为 vendored musl 打上 CVE-2026-6042 与 CVE-2026-40200 的补丁
平台支持
powerpc64-unknown-linux-musl提升为 Tier 2(含 host tools)aarch64-apple-tvos/tvos-sim/watchos/watchos-sim/visionos/visionos-sim提升为 Tier 2
库
thread::scope:文档说明join与 TLS 析构器的交互- 在默认启用 neon target feature 的 aarch64 目标上加速
str::contains
兼容性注意
- 数组强制转换现在可能产生比之前更少(更宽松)的推断约束
use $crate::{self};这种不带重命名导入$crate的写法不再被允许(更严格的self导入错误检查)- const-eval 对类型化拷贝 padding 行为更一致:极少数情况下,指针部分的字节落入 const/static 的 padding 字节,可能导致编译错误
- 新增 future-incompatibility lint
ambiguous_glob_imported_traits,在使用有歧义的 glob 导入 trait 时报出 - 检查仅提及类型参数的类型生命周期边界
- 报告更多与可见性相关的歧义导入错误
- 弃用
Eq::assert_receiver_is_total_eq,并对手动实现发出 future compatibility 警告 - powerpc64:使用 target spec 中设置的 ELF ABI 版本而非猜测
- 匹配
#[non_exhaustive]枚举现在会读取 discriminant,即使枚举只有一个变体;这可能导致闭包捕获此前不会捕获的值 mut ref与mut ref mut模式(不稳定 Match Ergonomics 2024 RFC 的一部分)此前在 struct pattern field 简写中被意外允许稳定使用,现在正确 feature-gate 为不稳定- 为与内置属性冲突的 derive helper 属性添加 future-compatibility 警告
- JSON target specs 被 destabilize,现在需要
-Z unstable-options才能使用。此前它们无法在没有标准库的情况下使用,而标准库没有稳定构建机制。为 build-std 项目做准备,JSON target specs 被提前 gate,以确保即便 build-std 稳定后它们仍不稳定。Cargo 新增-Z json-target-specCLI flag,在需要时自动传-Z unstable-options给编译器。见 #150151、#151534、rust-lang/cargo#16557 - 无效 target 上
#[feature]属性的参数现在会被检查
内部变化
- 升级到 LLVM 22