expression

Infinite iterators — bound the consumer

Some iterator sources never report exhaustion. Limit their consumption with take or another guaranteed stopping rule before collecting. Short-circuit searches can still run forever if no match exists. Expected stdout (not executed): ["pending", "pending", "pending"] Common mistake: Collecting repeat directly into a Vec.

Formal Syntax & Structure

rust syntaxrust
std::iter::repeat(value).take(limit)

Working Example

Example Snippetrust
fn main() {    let labels: Vec<_> = std::iter::repeat("pending").take(3).collect();    println!("{labels:?}");}
Rust Reference