rust-lang / rust-lang/rust-clippy

New lint: vec.extend(&vec![elem; len]) is slow, vec.resize() should be used instead

Open
#4,660 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint C-enhancement L-perf L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

There are some very slow ways of extending a vector with len new elements:

vec.extend(&vec![elem; len]);
vec.extend_from_slice(&vec![elem; len]);

Instead it should be written like this:

vec.resize(vec.len() + len, elem);

This sounds like code that nobody would ever write, but it actually has real-world precedent: https://github.com/Frommi/miniz_oxide/pull/55

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing existing rust-clippy lint implementations and the miniz_oxide pull request linked in the issue. Locate the lint entry point and related tests, then verify that both inefficient vector-extension forms are detected and the resize form is accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.