RU

Rust Performance

Rust performance patterns

Details

Language / Topic
rustRust
Category
Performance

Rules

balanced
- Use `&str` and `&[T]` (borrowed slices) to avoid unnecessary cloning and allocation.
- Compile with `--release` for optimized builds. Debug builds are 10-100x slower.
- Use `cargo bench` with Criterion.rs for benchmarks — compare against baselines to detect regressions across commits.
- Use iterators and combinators instead of indexed loops — they often optimize to the same assembly.
- Use `Vec::with_capacity(n)` when the final size is known to avoid reallocation.
- Use `Cow<str>` for functions that sometimes need to allocate and sometimes can borrow.
- Use `rayon` for data parallelism: `.par_iter()` for parallel map/filter/reduce.