rust-systems
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Systems Programming Best Practices
Rust系统编程最佳实践
Comprehensive Rust patterns and style conventions for systems programming, containing 52 rules across 5 categories. Designed for systems programming, CLI tools, and performance-critical applications.
一套全面的系统编程Rust模式与风格规范,包含5个类别下的52条规则。适用于系统编程、CLI工具和性能关键型应用场景。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Rust code or modules
- Organizing Rust project structure
- Defining custom types, traits, or error handling
- Reviewing Rust code for style consistency
- Building systems tools, CLIs, or daemon processes
在以下场景中参考本指南:
- 编写新的Rust代码或模块
- 组织Rust项目结构
- 定义自定义类型、trait或错误处理逻辑
- 评审Rust代码以保证风格一致性
- 构建系统工具、CLI或守护进程
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Project Organization | HIGH | |
| 2 | Module Structure | HIGH | |
| 3 | Naming Conventions | HIGH | |
| 4 | Type & Trait Patterns | HIGH | |
| 5 | Error Handling | HIGH | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 项目组织 | 高 | |
| 2 | 模块结构 | 高 | |
| 3 | 命名规范 | 高 | |
| 4 | 类型与Trait模式 | 高 | |
| 5 | 错误处理 | 高 | |
Quick Reference
快速参考
1. Project Organization (HIGH)
1. 项目组织(高优先级)
- - Use Cargo Workspace for Multi-Crate Projects
org-cargo-workspace - - Use snake_case for All Directory Names
org-directory-naming - - Separate Binary and Library Crates
org-binary-library-separation - - Group Crates by Feature Domain
org-feature-domain-grouping - - Use Dedicated Common Crate for Shared Utilities
org-common-crate - - Keep Crate Structure Flat
org-flat-crate-structure
- - 为多crate项目使用Cargo Workspace
org-cargo-workspace - - 所有目录名称使用snake_case
org-directory-naming - - 分离二进制与库Crate
org-binary-library-separation - - 按功能领域分组Crate
org-feature-domain-grouping - - 使用专用公共Crate存放共享工具
org-common-crate - - 保持Crate结构扁平化
org-flat-crate-structure
2. Module Structure (HIGH)
2. 模块结构(高优先级)
- - Use Explicit Module Declarations in lib.rs
mod-explicit-declarations - - Co-locate Tests as test.rs Files
mod-colocated-tests - - Use mod.rs for Multi-File Modules
mod-submodule-organization - - Separate Types and Errors into Dedicated Files
mod-types-errors-files - - Use pub use for Clean API Re-exports
mod-reexport-pattern - - Use cfg Attributes for Conditional Modules
mod-conditional-compilation
- - 在lib.rs中使用显式模块声明
mod-explicit-declarations - - 将测试代码放在同名test.rs文件中
mod-colocated-tests - - 为多文件模块使用mod.rs
mod-submodule-organization - - 将类型与错误分离到专用文件中
mod-types-errors-files - - 使用pub use实现清晰的API重导出
mod-reexport-pattern - - 使用cfg属性实现条件编译模块
mod-conditional-compilation
3. Naming Conventions (HIGH)
3. 命名规范(高优先级)
- - Use snake_case for Functions and Methods
name-function-snake-case - - Use PascalCase for Types
name-type-pascal-case - - Use SCREAMING_SNAKE_CASE for Constants
name-constant-screaming - - Prefix Getter Functions with get_
name-getter-prefix - - Use is_, has_, should_ for Boolean Predicates
name-boolean-predicates - - Use new for Constructors
name-constructor-new - - Use to_ and from_ for Conversions
name-conversion-to-from - - Use Descriptive Suffixes for Type Specialization
name-type-suffixes - - Include Unit Suffixes in Field Names
name-field-unit-suffixes - - Use snake_case for Module Names
name-module-snake-case - - Use Descriptive or Single-Letter Generic Parameters
name-generic-parameters - - Use Single Lowercase Letters for Lifetimes
name-lifetime-parameters - - Name Test Files as test.rs
name-test-files
- - 函数与方法使用snake_case
name-function-snake-case - - 类型使用PascalCase
name-type-pascal-case - - 常量使用SCREAMING_SNAKE_CASE
name-constant-screaming - - Getter函数前缀使用get_
name-getter-prefix - - 布尔断言使用is_、has_、should_前缀
name-boolean-predicates - - 构造函数使用new命名
name-constructor-new - - 转换函数使用to_和from_前缀
name-conversion-to-from - - 为专用类型使用描述性后缀
name-type-suffixes - - 字段名称中包含单位后缀
name-field-unit-suffixes - - 模块名称使用snake_case
name-module-snake-case - - 泛型参数使用描述性名称或单字母
name-generic-parameters - - 生命周期参数使用单个小写字母
name-lifetime-parameters - - 测试文件命名为test.rs
name-test-files
4. Type & Trait Patterns (HIGH)
4. 类型与Trait模式(高优先级)
- - Use Option<T> for Nullable Fields
type-option-nullable-fields - - Use Consistent Derive Order for Data Structs
type-standard-derives - - Use Builder Pattern with Method Chaining
type-builder-pattern - - Use Associated Types for Related Type Relationships
type-associated-types - - Use PhantomData for Unused Generic Parameters
type-phantom-data - - Use Newtype Pattern for Type Safety
type-newtype-pattern - - Derive Copy for Simple Enums
type-enum-copy-simple - - Use Enums for Type-Safe Variants
type-enum-variants - - Group Related Trait Implementations Together
type-trait-impl-grouping - - Use bitflags! for Type-Safe Bit Flags
type-bitflags - - Implement Operator Traits for Domain Types
type-operator-overload - - Use Public Fields for Data Structs
type-public-fields - - Use async_trait for Async Trait Methods
type-async-trait - - Use Box<dyn Trait> for Runtime Polymorphism
type-boxed-trait-objects - - Use Type Aliases for Complex Generics
type-type-aliases
- - 可为空字段使用Option<T>
type-option-nullable-fields - - 数据结构体使用一致的Derive顺序
type-standard-derives - - 使用带方法链的构建器模式
type-builder-pattern - - 为相关类型关系使用关联类型
type-associated-types - - 为未使用的泛型参数使用PhantomData
type-phantom-data - - 使用Newtype模式保证类型安全
type-newtype-pattern - - 为简单枚举派生Copy trait
type-enum-copy-simple - - 使用枚举实现类型安全的变体
type-enum-variants - - 将相关Trait实现分组存放
type-trait-impl-grouping - - 使用bitflags!实现类型安全的位标志
type-bitflags - - 为领域类型实现操作符Trait
type-operator-overload - - 数据结构体使用公共字段
type-public-fields - - 为异步Trait方法使用async_trait
type-async-trait - - 使用Box<dyn Trait>实现运行时多态
type-boxed-trait-objects - - 使用类型别名简化复杂泛型
type-type-aliases
5. Error Handling (HIGH)
5. 错误处理(高优先级)
- - Use thiserror for Custom Error Types
err-thiserror-enum - - Define Module-Local Result Type Alias
err-result-alias - - Include Path Context in IO Errors
err-path-context - - Use context() and with_context() for Error Messages
err-anyhow-context - - Use bail! for Validation Failures
err-bail-validation - - Use Graceful Degradation for Non-Critical Operations
err-graceful-degradation - - Reserve panic! for Unrecoverable Situations
err-panic-unrecoverable - - Use expect() with Descriptive Messages
err-expect-message - - Use #[source] for Error Chaining
err-source-attribute - - Use ok_or_else for Expensive Error Construction
err-ok-or-else - - Use Two-Tier Error Strategy
err-two-tier-strategy
- - 使用thiserror定义自定义错误类型
err-thiserror-enum - - 定义模块本地的Result类型别名
err-result-alias - - 在IO错误中包含路径上下文
err-path-context - - 使用context()和with_context()添加错误信息
err-anyhow-context - - 使用bail!处理验证失败
err-bail-validation - - 非关键操作使用优雅降级策略
err-graceful-degradation - - 仅在不可恢复场景下使用panic!
err-panic-unrecoverable - - 使用expect()并添加描述性信息
err-expect-message - - 使用#[source]实现错误链
err-source-attribute - - 使用ok_or_else处理开销较大的错误构造
err-ok-or-else - - 使用双层错误处理策略
err-two-tier-strategy
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单独的参考文件获取详细说明与代码示例:
- 章节定义 - 类别结构与影响程度说明
- 规则模板 - 添加新规则的模板
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded: AGENTS.md
如需查看包含所有规则扩展说明的完整指南:AGENTS.md