Clippy lints / Performance
Clippy lint · Performance · checked with Clippy 0.1.99
clippy::manual_str_repeat: manual implementation of `str::repeat` using iterators
What it meansChecks for manual implementations of str::repeat It is in the performance group and warns by default.
What you see
warning: manual implementation of `str::repeat` using iterators --> main.rs:3:17 | 3 | let x: String = std::iter::repeat('x').take(10).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"x".repeat(10)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#manual_str_repeat = note: `#[warn(clippy::manual_str_repeat)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
These are both harder to read, as well as less performant.
Code that could be written to run faster. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let x: String = std::iter::repeat('x').take(10).collect();
Write this instead
let x: String = "x".repeat(10);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::manual_str_repeat)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
manual_str_repeat = "allow"One run
cargo clippy -- -A clippy::manual-str-repeatThe [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
Clippy's own documentation
What it does
Checks for manual implementations of str::repeat
Why is this bad?
These are both harder to read, as well as less performant.
Example
let x: String = std::iter::repeat('x').take(10).collect();
Use instead:
let x: String = "x".repeat(10);
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.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
- large_enum_variantlarge size difference between variants
- manual_ignore_case_cmpmanual case-insensitive ASCII comparison
- manual_memcpyChecks for for-loops that manually copy items between slices that could be optimized by having a memcpy
- manual_retainthis expression can be written more simply using `.retain()`
- manual_try_foldusage of `Iterator::fold` on a type that implements `Try`
- map_entryusage of `contains_key` followed by `insert` on a `HashMap`
- missing_const_for_thread_localinitializer for `thread_local` value can be made `const`
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?