Implemting Iterator

If a type is implemented iterator, into_iterator is implemented automatically for it because of the following blanket implementation:


#![allow(unused)]
fn main() {
impl<I: ~const Iterator> const IntoIterator for I {
    type Item = I::Item;
    type IntoIter = I;

    #[inline]
    fn into_iter(self) -> I {
        self
    }
}
}

Sample Iterator implementation

use std::option::IterMut;

#[derive(Debug)]
struct Counter {
    count : i32
}

impl Counter {
    fn new () -> Self {
        Self { count : 0}
    }
}

impl Iterator for Counter {
    type Item = i32;

    fn next(&mut self) -> Option<Self::Item> {
        self.count += 1;
        if self.count == 17 {
            None
        } else {
            Some(self.count)
        }
    }
}

fn main() {
    let counter = Counter::new();

    for i in counter {
        println!("{i}");
    }

}