How to Use Vec

:with_capacity for Performance

Pre-allocate vector memory with Vec::with_capacity to prevent performance penalties from repeated reallocations.

Use Vec::with_capacity(n) to pre-allocate memory for a known number of elements, avoiding costly reallocations during growth.

let size = 100;
let mut workers = Vec::with_capacity(size);

for id in 0..size {
    workers.push(Worker::new(id));
}

This reserves space for size items upfront, so push operations inside the loop do not trigger memory reallocation or data copying.