install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/rust-optimise" ~/.claude/skills/comeonoliver-skillshub-rust-optimise && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/rust-optimise/SKILL.mdsource content
Rust Optimise Best Practices
Performance optimization guide for Rust applications. Contains 42 rules across 8 categories, prioritized by impact from critical (memory allocation, ownership) to incremental (micro-optimizations).
When to Apply
- Optimizing Rust code for performance or reducing allocations
- Choosing data structures for optimal algorithmic complexity
- Working with iterators to avoid unnecessary intermediate allocations
- Writing async code with Tokio or other runtimes
- Profiling hot paths and eliminating performance bottlenecks
- Reviewing code for performance anti-patterns
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 | |
Quick Reference
1. Memory Allocation (CRITICAL)
- Avoid unnecessary clone callsmem-avoid-unnecessary-clone
- Preallocate Vec capacitymem-preallocate-vec-capacity
- Use Cow for conditional ownershipmem-use-cow-for-conditional-ownership
- Use Arc for shared immutable datamem-use-arc-for-shared-immutable-data
- Avoid format! for simple concatenationmem-avoid-format-for-simple-concatenation
- Use SmallVec for small collectionsmem-use-smallvec-for-small-collections
2. Ownership & Borrowing (CRITICAL)
- Accept &str instead of &Stringown-accept-str-slice-not-string
- Accept &[T] instead of &Vec<T>own-accept-slice-not-vec
- Use Into<T> for flexible ownershipown-use-into-for-flexible-ownership
- Return borrowed data when possibleown-return-borrowed-when-possible
- Use AsRef<T> for generic borrowsown-use-asref-for-generic-borrows
3. Data Structure Selection (HIGH)
- Use HashSet for membership testsds-use-hashset-for-membership
- Use HashMap for key-value lookupsds-use-hashmap-for-key-lookup
- Use BTreeMap for sorted iterationds-use-btreemap-for-sorted-iteration
- Use VecDeque for queue operationsds-use-vecdeque-for-queue-operations
- Use Entry API for conditional insertds-use-entry-api-for-conditional-insert
4. Iterator & Collection Patterns (HIGH)
- Chain iterators instead of intermediate collectiter-chain-instead-of-intermediate-collect
- Use iter() over into_iter() when borrowingiter-use-iter-over-into-iter-when-borrowing
- Use filter_map for combined operationsiter-use-filter-map-for-combined-operations
- Use flat_map for nested iterationiter-use-flat-map-for-nested-iteration
- Use extend() for bulk appenditer-use-extend-for-bulk-append
- Use fold() for complex accumulationiter-use-fold-for-accumulation
5. Async & Concurrency (MEDIUM-HIGH)
- Avoid blocking in async contextasync-avoid-blocking-in-async-context
- Use join! for concurrent futuresasync-use-join-for-concurrent-futures
- Use RwLock over Mutex for read-heavyasync-use-rwlock-over-mutex-for-read-heavy
- Minimize lock scopeasync-minimize-lock-scope
- Use buffered() for bounded concurrencyasync-use-buffered-for-bounded-concurrency
- Avoid holding lock across awaitasync-avoid-holding-lock-across-await
6. Algorithm Complexity (MEDIUM)
- Avoid nested loops for lookupsalgo-avoid-nested-loops-for-lookup
- Use binary search for sorted dataalgo-use-binary-search-for-sorted-data
- Use sort_unstable when order irrelevantalgo-use-sort-unstable-when-order-irrelevant
- Use select_nth_unstable for partial sortalgo-use-select-nth-unstable-for-partial-sort
- Use chunks() for batch processingalgo-use-chunks-for-batch-processing
7. Compile-Time Optimization (MEDIUM)
- Use const for compile-time computationcomp-use-const-for-compile-time-computation
- Prefer static dispatch over dynamiccomp-prefer-static-dispatch
- Reduce monomorphization bloatcomp-reduce-monomorphization-bloat
- Use const generics for array sizescomp-use-const-generics-for-array-sizes
- Avoid repeated parsing of static datacomp-avoid-repeated-parsing-of-static-data
8. Micro-optimizations (LOW)
- Apply inline attribute to small hot functionsmicro-use-inline-for-small-functions
- Avoid bounds checks in hot loopsmicro-avoid-bounds-checks-in-hot-loops
- Use wrapping arithmetic when safemicro-use-wrapping-arithmetic-when-safe
- Use byte literals for ASCIImicro-use-byte-literals-for-ascii
References
- https://nnethercote.github.io/perf-book/
- https://rust-lang.github.io/api-guidelines/
- https://doc.rust-lang.org/nomicon/
- https://tokio.rs/tokio/tutorial
Related Skills
- For idiomatic patterns, architecture, and code organization, see
skillrust-refactor