rust-optimise
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 | mem- |
| 2 | Ownership & Borrowing | CRITICAL | own- |
| 3 | Data Structure Selection | HIGH | ds- |
| 4 | Iterator & Collection Patterns | HIGH | iter- |
| 5 | Async & Concurrency | MEDIUM-HIGH | async- |
| 6 | Algorithm Complexity | MEDIUM | algo- |
| 7 | Compile-Time Optimization | MEDIUM | comp- |
| 8 | Micro-optimizations | LOW | micro- |
Quick Reference
1. Memory Allocation (CRITICAL)
mem-avoid-unnecessary-clone- Avoid unnecessary clone callsmem-preallocate-vec-capacity- Preallocate Vec capacitymem-use-cow-for-conditional-ownership- Use Cow for conditional ownershipmem-use-arc-for-shared-immutable-data- Use Arc for shared immutable datamem-avoid-format-for-simple-concatenation- Avoid format! for simple concatenationmem-use-smallvec-for-small-collections- Use SmallVec for small collections
2. Ownership & Borrowing (CRITICAL)
own-accept-str-slice-not-string- Accept &str instead of &Stringown-accept-slice-not-vec- Accept &[T] instead of &Vecown-use-into-for-flexible-ownership- Use Into for flexible ownershipown-return-borrowed-when-possible- Return borrowed data when possibleown-use-asref-for-generic-borrows- Use AsRef for generic borrows
3. Data Structure Selection (HIGH)
ds-use-hashset-for-membership- Use HashSet for membership testsds-use-hashmap-for-key-lookup- Use HashMap for key-value lookupsds-use-btreemap-for-sorted-iteration- Use BTreeMap for sorted iterationds-use-vecdeque-for-queue-operations- Use VecDeque for queue operationsds-use-entry-api-for-conditional-insert- Use Entry API for conditional insert
4. Iterator & Collection Patterns (HIGH)
iter-chain-instead-of-intermediate-collect- Chain iterators instead of intermediate collectiter-use-iter-over-into-iter-when-borrowing- Use iter() over into_iter() when borrowingiter-use-filter-map-for-combined-operations- Use filter_map for combined operationsiter-use-flat-map-for-nested-iteration- Use flat_map for nested iterationiter-use-extend-for-bulk-append- Use extend() for bulk appenditer-use-fold-for-accumulation- Use fold() for complex accumulation
5. Async & Concurrency (MEDIUM-HIGH)
async-avoid-blocking-in-async-context- Avoid blocking in async contextasync-use-join-for-concurrent-futures- Use join! for concurrent futuresasync-use-rwlock-over-mutex-for-read-heavy- Use RwLock over Mutex for read-heavyasync-minimize-lock-scope- Minimize lock scopeasync-use-buffered-for-bounded-concurrency- Use buffered() for bounded concurrencyasync-avoid-holding-lock-across-await- Avoid holding lock across await
6. Algorithm Complexity (MEDIUM)
algo-avoid-nested-loops-for-lookup- Avoid nested loops for lookupsalgo-use-binary-search-for-sorted-data- Use binary search for sorted dataalgo-use-sort-unstable-when-order-irrelevant- Use sort_unstable when order irrelevantalgo-use-select-nth-unstable-for-partial-sort- Use select_nth_unstable for partial sortalgo-use-chunks-for-batch-processing- Use chunks() for batch processing
7. Compile-Time Optimization (MEDIUM)
comp-use-const-for-compile-time-computation- Use const for compile-time computationcomp-prefer-static-dispatch- Prefer static dispatch over dynamiccomp-reduce-monomorphization-bloat- Reduce monomorphization bloatcomp-use-const-generics-for-array-sizes- Use const generics for array sizescomp-avoid-repeated-parsing-of-static-data- Avoid repeated parsing of static data
8. Micro-optimizations (LOW)
micro-use-inline-for-small-functions- Apply inline attribute to small hot functionsmicro-avoid-bounds-checks-in-hot-loops- Avoid bounds checks in hot loopsmicro-use-wrapping-arithmetic-when-safe- Use wrapping arithmetic when safemicro-use-byte-literals-for-ascii- 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
rust-refactorskill
More from pproenca/dot-skills
zod
Zod schema validation best practices for type safety, parsing, and error handling. This skill should be used when defining z.object schemas, using z.string validations, safeParse, or z.infer. This skill does NOT cover React Hook Form integration patterns (use react-hook-form skill) or OpenAPI client generation (use orval skill).
2.0Kclean-architecture
Clean Architecture principles and best practices from Robert C. Martin's book. This skill should be used when designing software systems, reviewing code structure, or refactoring applications to achieve better separation of concerns. Triggers on tasks involving layers, boundaries, dependency direction, entities, use cases, or system architecture.
1.4Kemilkowal-animations
Emil Kowalski's animation best practices for web interfaces. Use when writing, reviewing, or implementing animations in React, CSS, or Framer Motion. Triggers on tasks involving transitions, easing, gestures, toasts, drawers, or motion.
918vitest
Vitest testing framework patterns for test setup, async testing, mocking with vi.*, snapshots, and test performance (formerly test-vitest). This skill should be used when writing or debugging Vitest tests. This skill does NOT cover TDD methodology (use test-tdd skill), API mocking with MSW (use test-msw skill), or Jest-specific APIs.
907typescript
This skill should be used when the user asks to "optimize TypeScript performance", "speed up tsc compilation", "configure tsconfig.json", "fix type errors", "improve async patterns", or encounters TS errors (TS2322, TS2339, "is not assignable to"). Also triggers on .ts, .tsx, .d.ts file work involving type definitions, module organization, or memory management. Does NOT cover TypeScript basics, framework-specific patterns, or testing.
821nuqs
nuqs (type-safe URL query state) best practices for Next.js applications. This skill should be used when writing, reviewing, or refactoring code that uses nuqs for URL state management. Triggers on tasks involving useQueryState, useQueryStates, search params, URL state, query parameters, nuqs parsers, or Next.js routing with state.
735