Email me

Clippy lints / Suspicious

Clippy lint · Suspicious · checked with Clippy 0.1.99

clippy::repeat_vec_with_capacity: Looks for patterns such as vec![Vec::with_capacity(x); n] or iter::repeat(Vec::with_capacity(x))

What it meansLooks for patterns such as vec![Vec::with_capacity(x); n] or iter::repeat(Vec::with_capacity(x)). It is in the suspicious group and warns by default.

Does it matter?

These constructs work by cloning the element, but cloning a Vec<_> does not respect the old vector’s capacity and effectively discards it. This makes iter::repeat(Vec::with_capacity(x)) especially suspicious because the user most certainly expected that the yielded Vec<_> will have the requested capacity, otherwise one can simply write iter::repeat(Vec::new()) instead and it will have the same effect. Similarly for vec![x; n], the element x is cloned to fill the vec. Unlike iter::repeat however, the vec repeat macro does not have to clone the value n times but just n - 1 times, because it can reuse the passed value for the last slot. That means that the last Vec<_> gets the requested capacity but all other ones do not.

Code that is most likely wrong or useless. Warns by default.

How to fix it

Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:

Flagged


let _: Vec<Vec<u8>> = vec![Vec::with_capacity(42); 123];
let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect();

Write this instead


let _: Vec<Vec<u8>> = iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect();
//                                      ^^^ this closure executes 123 times
//                                          and the vecs will have the expected capacity

Turn it off, or on

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

On one item

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

Whole crate (Cargo.toml)

[lints.clippy]
repeat_vec_with_capacity = "allow"

One run

cargo clippy -- -A clippy::repeat-vec-with-capacity

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.

Settings in clippy.toml

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Suspiciousgroup
warndefault level
nofixed by cargo clippy --fix
1.76.0added in Rust

Clippy's own documentation

What it does

Looks for patterns such as vec![Vec::with_capacity(x); n] or iter::repeat(Vec::with_capacity(x)).

Why is this bad?

These constructs work by cloning the element, but cloning a Vec<_> does not respect the old vector’s capacity and effectively discards it.

This makes iter::repeat(Vec::with_capacity(x)) especially suspicious because the user most certainly expected that the yielded Vec<_> will have the requested capacity, otherwise one can simply write iter::repeat(Vec::new()) instead and it will have the same effect.

Similarly for vec![x; n], the element x is cloned to fill the vec. Unlike iter::repeat however, the vec repeat macro does not have to clone the value n times but just n - 1 times, because it can reuse the passed value for the last slot. That means that the last Vec<_> gets the requested capacity but all other ones do not.

Example


let _: Vec<Vec<u8>> = vec![Vec::with_capacity(42); 123];
let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect();

Use instead:


let _: Vec<Vec<u8>> = iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect();
//                                      ^^^ this closure executes 123 times
//                                          and the vecs will have the expected capacity

Configuration

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml

    (default: current version)

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::print_in_format_impluse of `println!` in `Display` implRead 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