name: rust description: Rust性能优化指南。该技能应在编写、审查或重构Rust代码时使用,以确保最优性能模式。触发于涉及内存分配、所有权、借用、迭代器、异步代码或性能优化的任务。
社区Rust最佳实践
Rust应用程序的综合性能优化指南。包含8个类别中的42条规则,按影响优先级排序,以指导自动重构和代码生成。
何时应用
在以下情况下参考这些指南:
- 编写新的Rust代码
- 优化内存分配和所有权模式
- 处理迭代器和集合
- 使用Tokio或其他运行时编写异步代码
- 审查代码的性能问题
按优先级分类的规则类别
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 内存分配 | 关键 | mem- |
| 2 | 所有权与借用 | 关键 | own- |
| 3 | 数据结构选择 | 高 | ds- |
| 4 | 迭代器与集合模式 | 高 | iter- |
| 5 | 异步与并发 | 中高 | async- |
| 6 | 算法复杂度 | 中 | algo- |
| 7 | 编译时优化 | 中 | comp- |
| 8 | 微优化 | 低 | micro- |
快速参考
1. 内存分配 (关键)
mem-avoid-unnecessary-clone- 避免不必要的克隆调用mem-preallocate-vec-capacity- 预分配Vec容量mem-use-cow-for-conditional-ownership- 使用Cow进行条件所有权mem-use-arc-for-shared-immutable-data- 使用Arc共享不可变数据mem-avoid-format-for-simple-concatenation- 避免使用format!进行简单连接mem-use-smallvec-for-small-collections- 使用SmallVec处理小集合
2. 所有权与借用 (关键)
own-accept-str-slice-not-string- 接受&str而非&Stringown-accept-slice-not-vec- 接受&[T]而非&Vec<T>own-use-into-for-flexible-ownership- 使用Into<T>实现灵活所有权own-return-borrowed-when-possible- 尽可能返回借用数据own-use-asref-for-generic-borrows- 使用AsRef<T>进行通用借用
3. 数据结构选择 (高)
ds-use-hashset-for-membership- 使用HashSet进行成员测试ds-use-hashmap-for-key-lookup- 使用HashMap进行键值查找ds-use-btreemap-for-sorted-iteration- 使用BTreeMap进行排序迭代ds-use-vecdeque-for-queue-operations- 使用VecDeque进行队列操作ds-use-entry-api-for-conditional-insert- 使用Entry API进行条件插入
4. 迭代器与集合模式 (高)
iter-chain-instead-of-intermediate-collect- 链式迭代器而非中间收集iter-use-iter-over-into-iter-when-borrowing- 借用时使用iter()而非into_iter()iter-use-filter-map-for-combined-operations- 使用filter_map进行组合操作iter-use-flat-map-for-nested-iteration- 使用flat_map进行嵌套迭代iter-use-extend-for-bulk-append- 使用extend()进行批量追加iter-use-fold-for-accumulation- 使用fold()进行复杂累加
5. 异步与并发 (中高)
async-avoid-blocking-in-async-context- 避免在异步上下文中阻塞async-use-join-for-concurrent-futures- 使用join!处理并发futureasync-use-rwlock-over-mutex-for-read-heavy- 读多写少时使用RwLock而非Mutexasync-minimize-lock-scope- 最小化锁范围async-use-buffered-for-bounded-concurrency- 使用buffered()进行有界并发async-avoid-holding-lock-across-await- 避免在await期间持有锁
6. 算法复杂度 (中)
algo-avoid-nested-loops-for-lookup- 避免嵌套循环进行查找algo-use-binary-search-for-sorted-data- 对排序数据使用二分查找algo-use-sort-unstable-when-order-irrelevant- 当顺序无关时使用sort_unstablealgo-use-select-nth-unstable-for-partial-sort- 使用select_nth_unstable进行部分排序algo-use-chunks-for-batch-processing- 使用chunks()进行批处理
7. 编译时优化 (中)
comp-use-const-for-compile-time-computation- 使用const进行编译时计算comp-prefer-static-dispatch- 优先使用静态分发而非动态comp-reduce-monomorphization-bloat- 减少单态化膨胀comp-use-const-generics-for-array-sizes- 使用常量泛型处理数组大小comp-avoid-repeated-parsing-of-static-data- 避免重复解析静态数据
8. 微优化 (低)
micro-use-inline-for-small-functions- 对小而热的函数应用inline属性micro-avoid-bounds-checks-in-hot-loops- 避免在热循环中进行边界检查micro-use-wrapping-arithmetic-when-safe- 安全时使用包装算术micro-use-byte-literals-for-ascii- 对ASCII使用字节字面量
如何使用
阅读单独的参考文件以获取详细解释和代码示例:
完整编译文档
有关所有规则的全面指南,请参阅AGENTS.md。
参考文件
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义和排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本和参考信息 |