Rust's collect::<Vec<_>>() memory leak footgun
Link rustmemoryperformance
February 3, 2026
Interesting discovery about Rust 1.76’s collect() optimization. When converting a vector of larger elements into smaller ones (e.g., 128-bit to 32-bit), the new vector reuses the original’s memory allocation, tripling capacity unnecessarily.
Solutions:
- Call
shrink_to_fit()after collecting - Use
Box<[T]>for fixed-length collections - Consider arena allocation patterns