Infinite iteration is usually an error in most cases, leading to programs that never terminate. While there are some acceptable use cases such as
event streams, developers often unintentionally create infinite loops, causing resource exhaustion or unresponsive behavior.
Code examples
Noncompliant code example
use std::iter;
iter::repeat(1_u8).collect::<Vec<_>>(); // Noncompliant: This creates an infinite iterator.
Compliant solution
use std::iter;
iter::repeat(1_u8).take(5).collect::<Vec<_>>(); // Compliant: This creates a finite iterator by taking only the first 5 items.