Skip to content

附录 F:Rust 1.97 时代新特性清单(教程映射)

本附录基于本机 rustc 1.97.1 附带的 releases.md 与实测编译结果。
目标:回答「教程有没有跟上新特性」——版本号已对齐,下列特性现已补入正文/示例。

学习优先级(Web 后端)

优先级特性稳定大致版本教程位置状态
P0Edition 20241.85第 0 章已用
P0let-else更早第 3 章 / 示例已用
P0let chains1.88(2024 edition)第 1/3 章 + ch01/ch03已补
P0if let match guard1.95第 3 章 + ch03已补
P0Option::is_none_or / is_some_and1.70/1.82 一带第 5 章 + ch05已用并说明
P1async closures1.85第 10 章 + ch10已补
P1std::sync::LazyLock1.80+(后续持续增强)第 9 章 + ch09已补
P1RPIT + use<..> precise capturing1.82/1.87第 6 章 + ch06已补
P2exclusive range patterns a..b更早稳定完善第 1 章已补
P2trait object upcasting1.86第 6 章(概念)已补
P2offset_of!1.77+(后续收紧检查)第 13 章/附录概念提及
暂不教gen blocks / yield1.97 仍 experimental明确标注未稳
暂不教try { ... } 表达式1.97 仍 experimental明确标注未稳

1.97.0/1.97.1 本身更偏「硬化」

对入门后端影响较小(多为 lint/目标特性/边界语义)。学习上继续以:

  • Edition 2024 默认语义
  • let chains / if-let guards
  • async + tokio/axum 生态

为主即可。

实测(本机 1.97.1, edition 2024)

语法结果
if let ... && ... let chains通过
matchif let ... guard通过
async |x| ... async closure通过
impl Trait + use<'_>通过
LazyLock通过
gen { yield ... }失败(experimental)
try { ... }失败(experimental)

与旧教程写法的对照

rust
// 旧:嵌套 if let
if let Some(user) = find_user(id) {
    if user.active {
        // ...
    }
}

// 新(推荐,1.88+ / edition 2024):let chains
if let Some(user) = find_user(id) && user.active {
    // ...
}
rust
// 旧:guard 里只能用布尔表达式
match status {
    Some(code) if code == 200 => {}
    _ => {}
}

// 新(1.95+):if let guard
match payload {
    Some(raw) if let Ok(code) = raw.parse::<u16>() && code == 200 => {}
    _ => {}
}
rust
// 旧:lazy_static! / once_cell::sync::Lazy
// 新:标准库 LazyLock
use std::sync::LazyLock;
static CONFIG: LazyLock<String> = LazyLock::new(|| std::env::var("APP_ENV").unwrap_or_else(|_| "dev".into()));

明确不要写进生产示例的「看起来新」的东西

  1. gen / yield(尚未稳定)
  2. try 块表达式(尚未稳定)
  3. 依赖 nightly only 的 async gen / yeet 等

回归命令

bash
cd examples
cargo test --workspace
cargo run -p ch01_basics
cargo run -p ch03_structs_enums
cargo run -p ch06_traits
cargo run -p ch09_smart_pointers
cargo run -p ch10_async