Email me

Clippy lints / Performance

Clippy lint · Performance · checked with Clippy 0.1.99

clippy::slow_vector_initialization: Checks slow zero-filled vector initialization

What it meansChecks slow zero-filled vector initialization It is in the performance group and warns by default.

Does it matter?

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.

Code that could be written to run faster. Warns by default.

How to fix it

Clippy does not fix this one automatically. Change the code by hand:

Flagged

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));

Write this instead

let mut vec1 = vec![0; len];
let mut vec2 = vec![0; len];
let mut vec3 = vec![0; len];

Turn it off, or on

Allow it where it does not fit your code, as narrowly as you can:

On one item

#[allow(clippy::slow_vector_initialization)]
fn my_function() { /* ... */ }

Whole crate (Cargo.toml)

[lints.clippy]
slow_vector_initialization = "allow"

One run

cargo clippy -- -A clippy::slow-vector-initialization

The [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.

Performancegroup
warndefault level
nofixed by cargo clippy --fix
1.32.0added in Rust

Clippy's own documentation

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];

From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.

Related lints

All 847 Clippy lints · Rust compiler errors · Cargo.toml checker

Up next

Related lintclippy::large_enum_variantlarge size difference between variantsRead it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ