RS.CLIPPY.REPEAT_ONCE

Using `.repeat(1)` instead of `String.clone()`, `str.to_string()` or `slice.to_vec()`

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

What it does

Checks for usage of .repeat(1) and suggest the following method for each types.

  • .to_string() for str
  • .clone() for String
  • .to_vec() for slice

The lint will evaluate constant expressions and values as arguments of .repeat(..) and emit a message if they are equivalent to 1. (Related discussion in rust-clippy#7306)

Why is this bad?

For example, String.repeat(1) is equivalent to .clone(). If cloning the string is the intention behind this, clone() should be used.

Example

fn main() {
    let x = String::from("hello world").repeat(1);
}

Use instead:

fn main() {
    let x = String::from("hello world").clone();
}