rust-stable
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Stable Language Semantics
Rust稳定语言语义
Use this skill for language semantics: ownership, borrowing, lifetimes, traits, generics, pattern matching, error propagation, and Edition differences. First verify the project's actual toolchain and Minimum Supported Rust Version (MSRV).
For standard-library API selection (which collection, which smart pointer, which string type, which I/O trait), use . For concrete pattern examples ("how do I write X"), use .
rust-stdlibrust-by-example使用本skill处理语言语义:所有权、借用、生命周期、trait、泛型、模式匹配、错误传播和Edition差异。首先验证项目的实际工具链和最低支持Rust版本(MSRV)。
对于标准库API选择(使用哪个集合、哪个智能指针、哪个字符串类型、哪个I/O trait),使用。对于具体模式示例("如何编写X"),使用。
rust-stdlibrust-by-examplePrerequisites Before Starting
开始前的准备工作
- Run and
rustc --version --verbose.cargo --version - Inspect the field in
rust-version,rust-toolchain.toml, andrust-toolchain.Cargo.toml - Distinguish between three versions: local toolchain, project MSRV, and current official stable release.
- When encountering version-sensitive APIs, consult Current Stable Baseline and rely on official release notes and API documentation as the final authority.
- If a project is locked to an old version, strictly adhere only to syntaxes and APIs that are already stabilized for that specific version.
Current Offline Baseline: Rust 1.97.1 (released July 16, 2026). This is a dated repository baseline and does not update automatically.
- 运行和
rustc --version --verbose。cargo --version - 检查、
rust-toolchain.toml和rust-toolchain中的Cargo.toml字段。rust-version - 区分三个版本:本地工具链、项目MSRV和当前官方稳定版。
- 遇到版本敏感的API时,查阅当前稳定基线,并以官方发布说明和API文档作为最终权威。
- 如果项目锁定在旧版本,严格只使用该特定版本已稳定的语法和API。
当前离线基线: Rust 1.97.1(2026年7月16日发布)。这是固定日期的仓库基线,不会自动更新。
Capability Boundaries
能力边界
Suitable For Handling
适合处理
- Ownership, borrowing, lifetimes, and move semantics.
- ,
struct, pattern matching, traits, generics, and associated types.enum - ,
Optionerror propagation, and custom errors (the language mechanism).Result - Closures (/
Fn/FnMut), attribute syntax, format strings (the language mechanism).FnOnce - Modules, visibility modifiers, /
pub/pub(crate).pub(super) - Edition differences (language-level — , RPIT capture, match ergonomics).
unsafe extern - Common compilation errors, borrowing check failures, and stable migration judgments.
- 所有权、借用、生命周期和移动语义。
- 、
struct、模式匹配、trait、泛型和关联类型。enum - 、
Option错误传播和自定义错误(语言机制)。Result - 闭包(/
Fn/FnMut)、属性语法、格式化字符串(语言机制)。FnOnce - 模块、可见性修饰符、/
pub/pub(crate)。pub(super) - Edition差异(语言层面——、RPIT捕获、match ergonomics)。
unsafe extern - 常见编译错误、借用检查失败和稳定迁移判断。
Hand Off to Specialized Skills
移交给专门skill
| User Intent | Preferred Skill |
|---|---|
| "Which std collection / smart pointer / string type should I use?" | |
| "How do I write X in Rust?" (concrete pattern) | |
| API design (naming, traits, future-proofing, ~100 C-* rules) | |
| Project layout, module trees, workspace structure | |
| Cargo.toml dependencies, features, profiles, publishing | |
| Dependency governance, supply chain, cargo-deny | |
| Semver, breaking-change classification, publish workflow | |
| Java Maven/Gradle project migration and component replacement | |
| Java-to-Rust source-test parity, Rust obligations, and migration test audit | |
| Threads, locks, atomics, channels, Tokio | |
| Unit tests, integration tests, doctests, coverage | |
| Raw pointers, memory layout, FFI, Miri | |
| UniFFI-generated Kotlin, Swift, Python, Ruby, or WASM bindings | |
| macro_rules, derive, procedural macros | |
| Command contracts, standard streams, exit codes, CLI | |
| Server-side HTTP APIs, handlers, middleware | |
| SQL/ORMs, schemas, migrations, transactions, pools | |
| Web authentication, authorization, sessions, tokens | |
| Bare-metal firmware, no_std drivers, hardware validation | |
| Risk, correctness, API and safety review | |
| rustfmt, Clippy, Edition migration, lint policy | |
Do not replace the complete workflow of domain-specific skills with this skill.
| 用户意图 | 优先使用的Skill |
|---|---|
| "我应该使用哪个std集合/智能指针/字符串类型?" | |
| "如何在Rust中编写X?"(具体模式) | |
| API设计(命名、trait、面向未来、约100条C-*规则) | |
| 项目布局、模块树、workspace结构 | |
| Cargo.toml依赖、features、profiles、发布 | |
| 依赖治理、供应链、cargo-deny | |
| Semver、破坏性变更分类、发布工作流 | |
| Java Maven/Gradle项目迁移和组件替换 | |
| Java到Rust源代码-测试对等、Rust义务和迁移测试审计 | |
| 线程、锁、原子操作、通道、Tokio | |
| 单元测试、集成测试、doctest、覆盖率 | |
| 原始指针、内存布局、FFI、Miri | |
| UniFFI生成的Kotlin、Swift、Python、Ruby或WASM绑定 | |
| macro_rules、derive、过程宏 | |
| 命令契约、标准流、退出码、CLI | |
| 服务端HTTP API、处理器、中间件 | |
| SQL/ORM、模式、迁移、事务、连接池 | |
| Web认证、授权、会话、令牌 | |
| 裸机固件、no_std驱动、硬件验证 | |
| 风险、正确性、API和安全审查 | |
| rustfmt、Clippy、Edition迁移、lint策略 | |
不要用本skill替代领域专属skill的完整工作流。
Workflow
工作流
- Determine Version Boundaries — Record toolchain version, edition, MSRV, and target platform(s).
- Narrow Problem Scope — Determine whether the issue belongs to ownership/type system/standard library/compiler errors or a specialized domain.
- Read Minimal Resources — Open only references directly relevant to the problem; do not load entire documentation sets at once.
- Implement Minimum Correct Solution — Prioritize stable standard library usage, clear ownership semantics, and explicit error propagation.
- Run Quality Gate Checks — Execute ,
cargo fmt --check(with all targets/features), and relevant tests.cargo check - Handle Version Differences — If an API is not supported by MSRV, choose the old compatible API or implement a compatibility shim; otherwise explicitly raise the MSRV.
- Transfer to Specialized Skills — Load corresponding skills for async, unsafe macros, Web, embedded domains after entering those areas.
- 确定版本边界 — 记录工具链版本、edition、MSRV和目标平台。
- 缩小问题范围 — 确定问题属于所有权/类型系统/标准库/编译器错误还是专门领域。
- 阅读最少必要资源 — 只打开与问题直接相关的参考资料;不要一次加载全部文档。
- 实现最小正确解决方案 — 优先使用稳定的标准库、清晰的所有权语义和显式错误传播。
- 运行质量门禁检查 — 执行、
cargo fmt --check(所有目标和features)以及相关测试。cargo check - 处理版本差异 — 如果API不受MSRV支持,选择旧的兼容API或实现兼容垫片;否则显式提升MSRV。
- 移交给专门skill — 在进入async、unsafe、宏、Web、嵌入式等领域时,加载相应skill。
Design Rules
设计规则
- Prefer /
&T; clone only when ownership transfer is required or independent lifetimes are needed.&mut T - Use types to express invariants; prefer enums/newtypes over boolean parameters and unconstrained strings.
- Library code should return structured errors; application boundaries may add context before deciding how to display them.
- Prefer iterators and standard collections, but do not sacrifice readability for chain-of-call syntax.
- Do not use as a bypass for borrowing checks first prove that safe abstractions cannot express the requirement.
unsafe - Do not default third-party crate imports; compare against stable std, MSRV, maintenance cost, and supply chain risk.
- Never claim an API is stabilized in a specific version unless official release notes or an API page marked with confirm it.
since
- 优先使用/
&T;仅在需要所有权转移或独立生命周期时才使用clone。&mut T - 使用类型表达不变量;优先使用枚举/新类型而不是布尔参数和不受约束的字符串。
- 库代码应返回结构化错误;应用边界可在决定如何展示错误之前添加上下文。
- 优先使用迭代器和标准集合,但不要为链式调用语法牺牲可读性。
- 不要将用作绕过借用检查的捷径;首先证明安全抽象无法表达该需求。
unsafe - 不要默认导入第三方crate;需与稳定std、MSRV、维护成本和供应链风险进行比较。
- 除非官方发布说明或标记了的API页面确认,否则切勿声称某API在特定版本已稳定。
since
Validation Gateways
验证门禁
Run validation gates from lowest to highest risk:
bash
cargo fmt --all --check
cargo check --all-targets --all-features
cargo test --all-targets --all-features
cargo clippy --all-targets --all-features -- -D warningsIf the project does not support or contains platform-specific targets, record reasons and use a defined feature/target matrix. For unsafe code, add ; for MSRV issues, repeat and tests on the declared minimum toolchain.
--all-featurescargo miri testcargo check按从低到高的风险顺序运行验证门禁:
bash
cargo fmt --all --check
cargo check --all-targets --all-features
cargo test --all-targets --all-features
cargo clippy --all-targets --all-features -- -D warnings如果项目不支持或包含平台特定目标,则记录原因并使用定义的feature/目标矩阵。对于unsafe代码,添加;对于MSRV问题,在声明的最低工具链上重复和测试。
--all-featurescargo miri testcargo checkRequired Documentation to Read On-Demand
按需阅读的必备文档
Version & Language Semantics
版本与语言语义
- Current Stable Baseline: Latest version, compatibility notes, update steps.
- Ownership & Lifetimes: Borrowing design, return values and lifetime judgment rules.
- Traits & Generics: Bounds, associated types, trait objects, API trade-offs.
- Patterns & Idiomatic Style: Builder patterns, newtypes, RAII, typestate management.
- Style Guide: Naming conventions, module organization, documentation style, and API design principles.
- 当前稳定基线:最新版本、兼容性说明、更新步骤。
- 所有权与生命周期:借用设计、返回值与生命周期判断规则。
- Trait与泛型:约束、关联类型、trait对象、API权衡。
- 模式与惯用风格:Builder模式、新类型、RAII、typestate管理。
- 风格指南:命名约定、模块组织、文档风格和API设计原则。
Cross-references (load the right skill instead)
交叉参考(加载正确的skill)
- "Which std collection / smart pointer / string type?" → (its
rust-stdlibcovers collections, smart-pointers, string-types, iterators, I/O, interior-mutability, combinators, std-concurrency, process-and-fs, std-module-index)references/ - "How do I write X?" → (its
rust-by-examplecovers conversions, flow-control, closures, modules, generics-traits, error-handling, attributes, unsafe, procedural-macros-overview, inline-asm, migrating-from-other-languages)references/ - API design → ; Cargo manifest →
rust-api-design; dependency governance →rust-cargo-buildrust-dependencies
- "哪个std集合/智能指针/字符串类型?" → (其
rust-stdlib涵盖集合、智能指针、字符串类型、迭代器、I/O、内部可变性、组合器、std并发、进程与文件系统、std模块索引)references/ - "如何编写X?" → (其
rust-by-example涵盖转换、流程控制、闭包、模块、泛型-trait、错误处理、属性、unsafe、过程宏概述、内联汇编、从其他语言迁移)references/ - API设计 → ;Cargo清单 →
rust-api-design;依赖治理 →rust-cargo-buildrust-dependencies
Reproducible Examples
可复现示例
- Quick Start Workflows
- Ownership Patterns ()
examples/ownership-patterns.md - Collection Patterns ()
examples/collections-patterns.md - Trait Design Patterns ()
examples/trait-design-patterns.md - Error Handling Patterns ()
examples/error-handling-patterns.md - : Minimal golden examples compiled by repository CI.
examples/golden-basic/
- 快速入门工作流
- 所有权模式()
examples/ownership-patterns.md - 集合模式()
examples/collections-patterns.md - Trait设计模式()
examples/trait-design-patterns.md - 错误处理模式()
examples/error-handling-patterns.md - :由仓库CI编译的最小黄金示例。
examples/golden-basic/
Common Pitfalls to Avoid
需要避免的常见陷阱
- Confusing "current stable" with project MSRV.
- Running only , omitting tests, examples, benchmarks, or feature combinations.
cargo check - Blindly using , wrapping in
clone, or invokingArc<Mutex<_>>solely to avoid borrowing errors.unsafe - Holding synchronous locks across calls in async code.
.await - Describing versioned historical documentation as continuously updated resources.
- Copying code snippets without including dependencies, features, error types, and platform constraints.
- 混淆"当前稳定版"与项目MSRV。
- 只运行,省略测试、示例、基准或feature组合。
cargo check - 盲目使用、包装成
clone或调用Arc<Mutex<_>>来规避借用错误。unsafe - 在async代码中跨持有同步锁。
.await - 将带版本的历史文档描述为持续更新的资源。
- 复制代码片段时未包含依赖、features、错误类型和平台约束。
Official Sources
官方来源
Data Privacy Policy
数据隐私政策
This skill provides local knowledge, examples, and validation workflows only. It does not collect, store, or transmit user data. Access to official documentation must comply with the user's network access requirements prior to accessing public resources.
本skill仅提供本地知识、示例和验证工作流。它不收集、存储或传输用户数据。访问官方文档前,必须遵守用户的网络访问要求。