Looping over x.next()
is misleading and can introduce subtle bugs. x.next()
returns an Option
which should
be handled properly. When used in a loop, Option
implements IntoIterator
, resulting in unexpected behavior where only a
single element or None
might be processed, leading to difficult-to-diagnose errors.
Code examples
Noncompliant code example
for value in iterator.next() {
// Noncompliant: Looping directly over `iterator.next()`
println!("{value}");
}
Compliant solution
for value in iterator {
// Compliant: Looping directly over `iterator`
println!("{value}");
}
Alternatively, handling the Option:
if let Some(value) = iterator.next() {
// Process the single value
println!("{value}");
}