std-method

Iterator::cycle — repeat a clonable sequence

Repeat a sequence by restarting from a clone of its initial iterator. A nonempty finite source can become unbounded; use a stopping condition. An empty source stays empty. Expected stdout (not executed): ["A", "B", "A", "B", "A"] Common mistake: Collecting an unbounded cycle directly.

Formal Syntax & Structure

rust syntaxrust
fn cycle(self) -> Cycle<Self> where Self: Sized + Clone

Working Example

Example Snippetrust
fn main() {    let shifts: Vec<_> = ["A", "B"].into_iter().cycle().take(5).collect();    println!("{shifts:?}");}
Rust Reference