rust
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity Rust Best Practices
社区版Rust最佳实践
Comprehensive performance optimization guide for Rust applications. Contains 42 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
Rust应用程序综合性能优化指南。包含8个类别下的42条规则,按影响程度排序,可指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Rust code
- Optimizing memory allocation and ownership patterns
- Working with iterators and collections
- Writing async code with Tokio or other runtimes
- Reviewing code for performance issues
在以下场景中参考本指南:
- 编写新的Rust代码
- 优化内存分配和所有权模式
- 处理迭代器和集合
- 使用Tokio或其他运行时编写异步代码
- 审查代码以排查性能问题
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Memory Allocation | CRITICAL | |
| 2 | Ownership & Borrowing | CRITICAL | |
| 3 | Data Structure Selection | HIGH | |
| 4 | Iterator & Collection Patterns | HIGH | |
| 5 | Async & Concurrency | MEDIUM-HIGH | |
| 6 | Algorithm Complexity | MEDIUM | |
| 7 | Compile-Time Optimization | MEDIUM | |
| 8 | Micro-optimizations | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 内存分配 | 关键 | |
| 2 | 所有权与借用 | 关键 | |
| 3 | 数据结构选择 | 高 | |
| 4 | 迭代器与集合模式 | 高 | |
| 5 | 异步与并发 | 中高 | |
| 6 | 算法复杂度 | 中 | |
| 7 | 编译时优化 | 中 | |
| 8 | 微优化 | 低 | |
Quick Reference
快速参考
1. Memory Allocation (CRITICAL)
1. 内存分配(关键)
- - Avoid unnecessary clone calls
mem-avoid-unnecessary-clone - - Preallocate Vec capacity
mem-preallocate-vec-capacity - - Use Cow for conditional ownership
mem-use-cow-for-conditional-ownership - - Use Arc for shared immutable data
mem-use-arc-for-shared-immutable-data - - Avoid format! for simple concatenation
mem-avoid-format-for-simple-concatenation - - Use SmallVec for small collections
mem-use-smallvec-for-small-collections
- - 避免不必要的clone调用
mem-avoid-unnecessary-clone - - 预分配Vec容量
mem-preallocate-vec-capacity - - 使用Cow实现条件式所有权
mem-use-cow-for-conditional-ownership - - 使用Arc处理共享不可变数据
mem-use-arc-for-shared-immutable-data - - 简单字符串拼接避免使用format!
mem-avoid-format-for-simple-concatenation - - 小型集合使用SmallVec
mem-use-smallvec-for-small-collections
2. Ownership & Borrowing (CRITICAL)
2. 所有权与借用(关键)
- - Accept &str instead of &String
own-accept-str-slice-not-string - - Accept &[T] instead of &Vec<T>
own-accept-slice-not-vec - - Use Into<T> for flexible ownership
own-use-into-for-flexible-ownership - - Return borrowed data when possible
own-return-borrowed-when-possible - - Use AsRef<T> for generic borrows
own-use-asref-for-generic-borrows
- - 接受&str而非&String
own-accept-str-slice-not-string - - 接受&[T]而非&Vec<T>
own-accept-slice-not-vec - - 使用Into<T>实现灵活所有权
own-use-into-for-flexible-ownership - - 尽可能返回借用数据
own-return-borrowed-when-possible - - 使用AsRef<T>实现通用借用
own-use-asref-for-generic-borrows
3. Data Structure Selection (HIGH)
3. 数据结构选择(高)
- - Use HashSet for membership tests
ds-use-hashset-for-membership - - Use HashMap for key-value lookups
ds-use-hashmap-for-key-lookup - - Use BTreeMap for sorted iteration
ds-use-btreemap-for-sorted-iteration - - Use VecDeque for queue operations
ds-use-vecdeque-for-queue-operations - - Use Entry API for conditional insert
ds-use-entry-api-for-conditional-insert
- - 成员测试使用HashSet
ds-use-hashset-for-membership - - 键值查找使用HashMap
ds-use-hashmap-for-key-lookup - - 有序迭代使用BTreeMap
ds-use-btreemap-for-sorted-iteration - - 队列操作使用VecDeque
ds-use-vecdeque-for-queue-operations - - 条件插入使用Entry API
ds-use-entry-api-for-conditional-insert
4. Iterator & Collection Patterns (HIGH)
4. 迭代器与集合模式(高)
- - Chain iterators instead of intermediate collect
iter-chain-instead-of-intermediate-collect - - Use iter() over into_iter() when borrowing
iter-use-iter-over-into-iter-when-borrowing - - Use filter_map for combined operations
iter-use-filter-map-for-combined-operations - - Use flat_map for nested iteration
iter-use-flat-map-for-nested-iteration - - Use extend() for bulk append
iter-use-extend-for-bulk-append - - Use fold() for complex accumulation
iter-use-fold-for-accumulation
- - 链式调用迭代器而非中间collect
iter-chain-instead-of-intermediate-collect - - 借用时使用iter()而非into_iter()
iter-use-iter-over-into-iter-when-borrowing - - 组合操作使用filter_map
iter-use-filter-map-for-combined-operations - - 嵌套迭代使用flat_map
iter-use-flat-map-for-nested-iteration - - 批量追加使用extend()
iter-use-extend-for-bulk-append - - 复杂累积使用fold()
iter-use-fold-for-accumulation
5. Async & Concurrency (MEDIUM-HIGH)
5. 异步与并发(中高)
- - Avoid blocking in async context
async-avoid-blocking-in-async-context - - Use join! for concurrent futures
async-use-join-for-concurrent-futures - - Use RwLock over Mutex for read-heavy
async-use-rwlock-over-mutex-for-read-heavy - - Minimize lock scope
async-minimize-lock-scope - - Use buffered() for bounded concurrency
async-use-buffered-for-bounded-concurrency - - Avoid holding lock across await
async-avoid-holding-lock-across-await
- - 避免在异步上下文中阻塞
async-avoid-blocking-in-async-context - - 并发Future使用join!
async-use-join-for-concurrent-futures - - 读密集场景使用RwLock而非Mutex
async-use-rwlock-over-mutex-for-read-heavy - - 最小化锁作用域
async-minimize-lock-scope - - 有界并发使用buffered()
async-use-buffered-for-bounded-concurrency - - 避免在await期间持有锁
async-avoid-holding-lock-across-await
6. Algorithm Complexity (MEDIUM)
6. 算法复杂度(中)
- - Avoid nested loops for lookups
algo-avoid-nested-loops-for-lookup - - Use binary search for sorted data
algo-use-binary-search-for-sorted-data - - Use sort_unstable when order irrelevant
algo-use-sort-unstable-when-order-irrelevant - - Use select_nth_unstable for partial sort
algo-use-select-nth-unstable-for-partial-sort - - Use chunks() for batch processing
algo-use-chunks-for-batch-processing
- - 查找操作避免嵌套循环
algo-avoid-nested-loops-for-lookup - - 有序数据使用二分查找
algo-use-binary-search-for-sorted-data - - 无需稳定顺序时使用sort_unstable
algo-use-sort-unstable-when-order-irrelevant - - 部分排序使用select_nth_unstable
algo-use-select-nth-unstable-for-partial-sort - - 批量处理使用chunks()
algo-use-chunks-for-batch-processing
7. Compile-Time Optimization (MEDIUM)
7. 编译时优化(中)
- - Use const for compile-time computation
comp-use-const-for-compile-time-computation - - Prefer static dispatch over dynamic
comp-prefer-static-dispatch - - Reduce monomorphization bloat
comp-reduce-monomorphization-bloat - - Use const generics for array sizes
comp-use-const-generics-for-array-sizes - - Avoid repeated parsing of static data
comp-avoid-repeated-parsing-of-static-data
- - 编译时计算使用const
comp-use-const-for-compile-time-computation - - 优先使用静态分发而非动态分发
comp-prefer-static-dispatch - - 减少单态化膨胀
comp-reduce-monomorphization-bloat - - 数组大小使用const泛型
comp-use-const-generics-for-array-sizes - - 避免重复解析静态数据
comp-avoid-repeated-parsing-of-static-data
8. Micro-optimizations (LOW)
8. 微优化(低)
- - Apply inline attribute to small hot functions
micro-use-inline-for-small-functions - - Avoid bounds checks in hot loops
micro-avoid-bounds-checks-in-hot-loops - - Use wrapping arithmetic when safe
micro-use-wrapping-arithmetic-when-safe - - Use byte literals for ASCII
micro-use-byte-literals-for-ascii
- - 小型热点函数添加inline属性
micro-use-inline-for-small-functions - - 热点循环中避免边界检查
micro-avoid-bounds-checks-in-hot-loops - - 安全场景使用包装算术
micro-use-wrapping-arithmetic-when-safe - - ASCII字符使用字节字面量
micro-use-byte-literals-for-ascii
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 a comprehensive guide with all rules in a single document, see AGENTS.md.
如需包含所有规则的综合指南单文档,请查看AGENTS.md。
Reference Files
参考文件
| File | Description |
|---|---|
| AGENTS.md | Complete compiled guide with all rules |
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |