rust-expert-best-practices-code-review
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Expert Best Practices
Rust专家级最佳实践
Simple, pragmatic, opinionated. Only what matters for writing production-grade Rust code.
简洁、务实、有明确立场。仅涵盖编写生产级Rust代码的核心要点。
When to Apply
适用场景
Reference these guidelines when:
- Writing Rust code (structs, functions, enums, traits)
- Implementing error handling and Result types
- Reviewing Rust code for safety or performance issues
- Refactoring existing Rust codebases
- Designing APIs and public interfaces
- Optimizing Rust code for performance or clarity
在以下场景中可参考本指南:
- 编写Rust代码(结构体、函数、枚举、trait)
- 实现错误处理与Result类型
- 评审Rust代码的安全性或性能问题
- 重构现有Rust代码库
- 设计API与公共接口
- 优化Rust代码的性能或可读性
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Type Safety | CRITICAL | |
| 2 | Error Handling | CRITICAL-HIGH | |
| 3 | API Design | HIGH | |
| 4 | Code Quality | MEDIUM-HIGH | |
| 5 | Readability | MEDIUM | |
| 6 | Performance | MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 类型安全 | 关键 | |
| 2 | 错误处理 | 关键-高 | |
| 3 | API设计 | 高 | |
| 4 | 代码质量 | 中-高 | |
| 5 | 可读性 | 中 | |
| 6 | 性能 | 中 | |
Quick Reference
速查指南
- - Use &str, &[T], &Path instead of &String, &Vec<T>, &PathBuf
use-borrowed-argument-types - - Use exhaustive enum matching for safe deserialization
use-enum-deserialization - - Wrap index types to prevent mixing different indices
use-typesafe-index-wrappers - - Use ? operator instead of unwrap/expect in Result functions
result-error-returns - - Use assert!, Result, or expect based on context instead of panic!
avoid-panic - - Use iterator methods instead of explicit push loops
use-iterator-transforms - - Use .copied() to avoid complex dereferencing patterns
use-copied - - Use format! over manual string concatenation
prefer-format - - Use builder pattern for functions with 4+ parameters
prefer-builder-pattern-for-complex - - Use named placeholders instead of bare _ in destructuring
use-named-placeholders - - Use .is_sign_negative() instead of comparing to Decimal::ZERO
decimal-comparison - - Implement calculated fields as methods not struct fields
calculated-field-as-method - - Avoid unnecessary Rc<T> when simpler ownership patterns work
avoid-rc - - Don't use Box<T> for concrete types without legitimate reason
avoid-box - - Replace boolean parameters with enums or structs
avoid-boolean-params - - Explicitly handle all enum variants without catch-all patterns
match-statements-handle-all-cases
- - 使用&str、&[T]、&Path替代&String、&Vec<T>、&PathBuf
use-borrowed-argument-types - - 使用穷举枚举匹配实现安全的反序列化
use-enum-deserialization - - 对索引类型进行包装,避免混淆不同类型的索引
use-typesafe-index-wrappers - - 在Result函数中使用?运算符替代unwrap/expect
result-error-returns - - 根据上下文使用assert!、Result或expect,而非panic!
avoid-panic - - 使用迭代器方法替代显式的push循环
use-iterator-transforms - - 使用.copied()避免复杂的解引用模式
use-copied - - 使用format!替代手动字符串拼接
prefer-format - - 对参数数量≥4的函数使用构建器模式
prefer-builder-pattern-for-complex - - 在解构时使用命名占位符而非裸下划线_
use-named-placeholders - - 使用.is_sign_negative()替代与Decimal::ZERO的比较
decimal-comparison - - 将计算字段实现为方法而非结构体字段
calculated-field-as-method - - 当更简单的所有权模式可行时,避免不必要的Rc<T>
avoid-rc - - 无合理理由时,不要对具体类型使用Box<T>
avoid-box - - 用枚举或结构体替代布尔型参数
avoid-boolean-params - - 显式处理所有枚举变体,不要使用通配模式
match-statements-handle-all-cases
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/use-borrowed-argument-types.md
rules/use-enum-deserialization.md
rules/use-typesafe-index-wrappers.md
rules/result-error-returns.md
rules/avoid-panic.md
rules/use-iterator-transforms.md
rules/use-copied.md
rules/prefer-format.md
rules/prefer-builder-pattern-for-complex.md
rules/use-named-placeholders.md
rules/decimal-comparison.md
rules/calculated-field-as-method.md
rules/avoid-rc.md
rules/avoid-box.md
rules/avoid-boolean-params.md
rules/match-statements-handle-all-cases.mdEach rule file contains:
- Brief explanation of why it matters
- When to use and when not to use the pattern
- Implementation requirements
- BAD code examples with explanation
- GOOD code examples with explanation
- Additional context and best practices
阅读单个规则文件获取详细说明与代码示例:
rules/use-borrowed-argument-types.md
rules/use-enum-deserialization.md
rules/use-typesafe-index-wrappers.md
rules/result-error-returns.md
rules/avoid-panic.md
rules/use-iterator-transforms.md
rules/use-copied.md
rules/prefer-format.md
rules/prefer-builder-pattern-for-complex.md
rules/use-named-placeholders.md
rules/decimal-comparison.md
rules/calculated-field-as-method.md
rules/avoid-rc.md
rules/avoid-box.md
rules/avoid-boolean-params.md
rules/match-statements-handle-all-cases.md每个规则文件包含:
- 规则重要性的简要说明
- 规则的适用与不适用场景
- 实现要求
- 带有解释的错误代码示例
- 带有解释的正确代码示例
- 额外背景信息与最佳实践