关于 rust
面向 Rust 开发者和编码 Agent 的性能实践指南,按影响优先级整理规则,涵盖内存、所有权、集合、异步代码、算法与编译期优化。
主要功能
- 01按优先级划分八类指南
- 02内存分配与所有权规则
- 03集合与迭代器模式
- 04异步与并发实践
- 05算法与编译期优化建议
适用场景
- 01编写新的 Rust 代码
- 02检查代码中的性能问题
- 03优化内存与所有权模式
- 04处理 Rust 异步代码
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pproenca/ dot-skills --skill rust安装后在 Claude Code 中输入/rust确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
AGENTS.mdREADME.mdmetadata.jsonassets/templates/_template.mdreferences/_sections.mdalgo-avoid-nested-loops-for-lookup.mdalgo-use-binary-search-for-sorted-data.mdalgo-use-chunks-for-batch-processing.mdalgo-use-select-nth-unstable-for-partial-sort.mdalgo-use-sort-unstable-when-order-irrelevant.mdasync-avoid-blocking-in-async-context.mdasync-avoid-holding-lock-across-await.mdasync-minimize-lock-scope.mdasync-use-buffered-for-bounded-concurrency.mdasync-use-join-for-concurrent-futures.mdasync-use-rwlock-over-mutex-for-read-heavy.mdcomp-avoid-repeated-parsing-of-static-data.mdcomp-prefer-static-dispatch.mdcomp-reduce-monomorphization-bloat.mdcomp-use-const-for-compile-time-computation.mdcomp-use-const-generics-for-array-sizes.mdds-use-btreemap-for-sorted-iteration.mdds-use-entry-api-for-conditional-insert.mdds-use-hashmap-for-key-lookup.mdds-use-hashset-for-membership.mdds-use-vecdeque-for-queue-operations.mditer-chain-instead-of-intermediate-collect.mditer-use-extend-for-bulk-append.mditer-use-filter-map-for-combined-operations.mditer-use-flat-map-for-nested-iteration.md详细介绍
社区 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- 避免不必要的 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],而不是 &Vecown-use-into-for-flexible-ownership- 使用 Into 实现灵活的所有权own-return-borrowed-when-possible- 尽可能返回借用数据own-use-asref-for-generic-borrows- 使用 AsRef 实现泛型借用
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. 迭代器与集合模式(HIGH)
iter-chain-instead-of-intermediate-collect- 链接迭代器,而不是使用中间 collectiter-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. 异步与并发(MEDIUM-HIGH)
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. 算法复杂度(MEDIUM)
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. 编译时优化(MEDIUM)
comp-use-const-for-compile-time-computation- 使用 const 进行编译时计算comp-prefer-static-dispatch- 优先使用静态分发,而不是动态分发comp-reduce-monomorphization-bloat- 减少单态化膨胀comp-use-const-generics-for-array-sizes- 使用 const 泛型指定数组大小comp-avoid-repeated-parsing-of-static-data- 避免重复解析静态数据
8. 微优化(LOW)
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 | 版本和参考信息 |
评论
支持 Markdown