Efficient Collection Operations
Efficient Collection Operations
Preallocate capacity using Vec::with_capacity to prevent repeated reallocations:
fn main() {
let mut fast_vec = Vec::with_capacity(1000);
for i in 0..1000 {
fast_vec.push(i * 2);
}
println!("Length: {}, Capacity: {}", fast_vec.len(), fast_vec.capacity());
}