- break
Create a simple iterator to count boundless
examples/iterators/iterator-unlimited-counter/src/main.rs
#[derive(Debug)] //#[allow(dead_code)] struct Counter { current: u32, } impl Counter { fn new() -> Counter { Counter { current: 0, } } } impl Iterator for Counter { type Item = u32; fn next(&mut self) -> Option<Self::Item> { self.current += 1; Some(self.current) } } fn main() { let cnt = Counter::new(); println!("{:?}", &cnt); for x in cnt { println!("{}", x); if 10 <= x { break; } } }
Counter { current: 0 } 1 2 3 4 5 6 7 8 9 10