RS.CLIPPY.NEEDLESS_RANGE_LOOP
For-looping over a range of indices where an iterator over items 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: needless_range_loop. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for looping over the range of 0..len of some
collection just to get the values by index.
Why is this bad?
Just iterating the collection itself makes the intent more clear and is probably faster because it eliminates the bounds check that is done when indexing.
Example
let vec = vec![\'a\', \'b\', \'c\'];
for i in 0..vec.len() {
println!("{}", vec[i]);
}
Use instead:
let vec = vec![\'a\', \'b\', \'c\'];
for i in vec {
println!("{}", i);
}