RS.CLIPPY.SLOW_VECTOR_INITIALIZATION
Slow vector initialization
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: slow_vector_initialization. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks slow zero-filled vector initialization
Why is this bad?
These structures are non-idiomatic and less efficient than simply using
vec![0; len].
Specifically, for vec![0; len], the compiler can use a specialized type of allocation
that also zero-initializes the allocated memory in the same call
(see: alloc_zeroed).
Writing Vec::new() followed by vec.resize(len, 0) is suboptimal because,
while it does do the same number of allocations,
it involves two operations for allocating and initializing.
The resize call first allocates memory (since Vec::new() did not), and only then zero-initializes it.
Example
let mut vec1 = Vec::new();
vec1.resize(len, 0);
let mut vec2 = Vec::with_capacity(len);
vec2.resize(len, 0);
let mut vec3 = Vec::with_capacity(len);
vec3.extend(repeat(0).take(len));
Use instead:
let mut vec1 = vec![0; len];
let mut vec2 = vec![0; len];
let mut vec3 = vec![0; len];