用途:在编写或修改 Rust 代码时,按实际锁定的 rustc 版本选用对应 API,避免凭训练数据写出不存在的接口。适用于编辑 .rs、Cargo.toml/Cargo.lock、rust-toolchain.toml,以及编写 Rust 测试与宏。
主要能力:先确定 rustc 1.y 版本,再查阅对应 reference 文件(1.95 对应 references/1.95.md,1.96 对应 references/1.96.md,1.97 对应 references/1.97.md)。未列出的更新版本以已覆盖的最高档为底,再查 RELEASES.md 或 blog.rust-lang.org;更旧版本不使用本技能中的新 API。写代码只开当前档,1.97 含仍该用的 1.95/1.96 索引。
定版本方式:读已锁定工具链,不要只看 Cargo.toml 的 rust-version(那是 MSRV 下限)。优先看 rust-toolchain.toml 或 rust-toolchain 的 channel(两文件都在时,按 rustup 规则无后缀的 rust-toolchain 优先)。channel 为 stable/beta/nightly 时,再跑 rustc --version 取实际 1.y。没有工具链文件时用当前 rustc --version;仍没有才用 Cargo.toml 的 package.rust-version,并按该下限写,不要用更高档 API。取 1.y 时 1.97.1 记为 1.97。
edition:读 Cargo.toml 的 edition(workspace 可能在 [workspace.package])。rustc 新但 edition 仍是 2021 时,不要写 2024 才启用的语法。需要某 edition 特性时先改 edition,不要靠 #![feature]。
硬规则:let chains 用 if let Some(x) = a && let Ok(y) = b { },不要嵌套 if let;错误用 ? / let else / if let,不要把 unwrap()/expect() 当控制流(测试里可以);优先 std 与 Cargo.toml 已有 crate,不为已有功能再拉重复实现;模块、错误类型、测试框架跟项目走;unsafe 保持最小,紧邻写清 SAFETY。
限制:Axum/Actix/Rocket 等 web 框架不在本技能里指定,也不安利 web 框架或新异步运行时。