rust-lang / rust-lang/rust-clippy

Suggest using `String::extend` where appropriate

Open
#17,633 4 comments 0 reactions 1 assignee View on GitHub

@saberoueslati is already working on this.

Since Sep 17, 2026.

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

Description

What it does

The String type has some Extend implementations, and the lint suggests using those instead of pushing in a loop.

Advantage
  • More concise
  • I suspect it optimizes better
Drawbacks

No response

Example
for c in s.chars() {
    out_string.push(c);
}

Could be written as:

out_string.extend(s.chars());

And similar for e.g. String::push_str in a loop

Comparison with existing lints

The closest related lint I can find is string_extend_chars, which would replace the output for the example I provided with out_string.push_str(s);. For other Iterator<char>s (e.g. core::char::ToLowercase was the iterator I was using when inspired to make this issue ticket), the String::extend call would most likely be the last suggestion we can make.

Additional Context

It might be reasonable to extend to other data structures beyond just Strings.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.