RS.CLIPPY.EXPLICIT_ITER_LOOP

For-looping over `_.iter()` or `_.iter_mut()` when `&_` or `&mut _` would do

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

What it does

Checks for loops on x.iter() where &x will do, and suggests the latter.

Why is this bad?

Readability.

Known problems

False negatives. We currently only warn on some known types.

Example

// with `y` a `Vec` or slice:
for x in y.iter() {
    // ..
}

Use instead:

for x in &y {
    // ..
}

Configuration

  • enforce-iter-loop-reborrow: Whether to recommend using implicit into iter for reborrowed values.

Example

let mut vec = vec![1, 2, 3];
let rmvec = &mut vec;
for _ in rmvec.iter() {}
for _ in rmvec.iter_mut() {}

Use instead:

let mut vec = vec![1, 2, 3];
let rmvec = &mut vec;
for _ in &*rmvec {}
for _ in &mut *rmvec {}

(default: false)