RS.CLIPPY.MANUAL_REPEAT_N

Detect `repeat().take()` that can be replaced with `repeat_n()`

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: manual_repeat_n. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for repeat().take() that can be replaced with repeat_n().

Why is this bad?

Using repeat_n() is more concise and clearer. Also, repeat_n() is sometimes faster than repeat().take() when the type of the element is non-trivial to clone because the original value can be reused for the last .next() call rather than always cloning.

Example

let _ = std::iter::repeat(10).take(3);

Use instead:

let _ = std::iter::repeat_n(10, 3);

Configuration

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

    (default: current version)