rust-expert-best-practices-code-review

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Rust 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

按优先级划分的规则类别

PriorityCategoryImpactPrefix
1Type SafetyCRITICAL
use-typesafe-
,
use-enum-
2Error HandlingCRITICAL-HIGH
result-
,
avoid-panic
3API DesignHIGH
use-borrowed-
,
prefer-builder-
4Code QualityMEDIUM-HIGH
use-iterator-
,
prefer-format
5ReadabilityMEDIUM
use-named-
,
avoid-boolean-
6PerformanceMEDIUM
avoid-rc
,
avoid-box
优先级类别影响程度前缀
1类型安全关键
use-typesafe-
,
use-enum-
2错误处理关键-高
result-
,
avoid-panic
3API设计
use-borrowed-
,
prefer-builder-
4代码质量中-高
use-iterator-
,
prefer-format
5可读性
use-named-
,
avoid-boolean-
6性能
avoid-rc
,
avoid-box

Quick Reference

速查指南

  • use-borrowed-argument-types
    - Use &str, &[T], &Path instead of &String, &Vec<T>, &PathBuf
  • use-enum-deserialization
    - Use exhaustive enum matching for safe deserialization
  • use-typesafe-index-wrappers
    - Wrap index types to prevent mixing different indices
  • result-error-returns
    - Use ? operator instead of unwrap/expect in Result functions
  • avoid-panic
    - Use assert!, Result, or expect based on context instead of panic!
  • use-iterator-transforms
    - Use iterator methods instead of explicit push loops
  • use-copied
    - Use .copied() to avoid complex dereferencing patterns
  • prefer-format
    - Use format! over manual string concatenation
  • prefer-builder-pattern-for-complex
    - Use builder pattern for functions with 4+ parameters
  • use-named-placeholders
    - Use named placeholders instead of bare _ in destructuring
  • decimal-comparison
    - Use .is_sign_negative() instead of comparing to Decimal::ZERO
  • calculated-field-as-method
    - Implement calculated fields as methods not struct fields
  • avoid-rc
    - Avoid unnecessary Rc<T> when simpler ownership patterns work
  • avoid-box
    - Don't use Box<T> for concrete types without legitimate reason
  • avoid-boolean-params
    - Replace boolean parameters with enums or structs
  • match-statements-handle-all-cases
    - Explicitly handle all enum variants without catch-all patterns
  • use-borrowed-argument-types
    - 使用&str、&[T]、&Path替代&String、&Vec<T>、&PathBuf
  • use-enum-deserialization
    - 使用穷举枚举匹配实现安全的反序列化
  • use-typesafe-index-wrappers
    - 对索引类型进行包装,避免混淆不同类型的索引
  • result-error-returns
    - 在Result函数中使用?运算符替代unwrap/expect
  • avoid-panic
    - 根据上下文使用assert!、Result或expect,而非panic!
  • use-iterator-transforms
    - 使用迭代器方法替代显式的push循环
  • use-copied
    - 使用.copied()避免复杂的解引用模式
  • prefer-format
    - 使用format!替代手动字符串拼接
  • prefer-builder-pattern-for-complex
    - 对参数数量≥4的函数使用构建器模式
  • use-named-placeholders
    - 在解构时使用命名占位符而非裸下划线_
  • decimal-comparison
    - 使用.is_sign_negative()替代与Decimal::ZERO的比较
  • calculated-field-as-method
    - 将计算字段实现为方法而非结构体字段
  • avoid-rc
    - 当更简单的所有权模式可行时,避免不必要的Rc<T>
  • avoid-box
    - 无合理理由时,不要对具体类型使用Box<T>
  • 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.md
Each 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
每个规则文件包含:
  • 规则重要性的简要说明
  • 规则的适用与不适用场景
  • 实现要求
  • 带有解释的错误代码示例
  • 带有解释的正确代码示例
  • 额外背景信息与最佳实践