#![allow(dead_code, deprecated, unused_variables, unused_mut)] #[derive(Debug)] struct MyCollection(Vec); impl MyCollection { fn new() -> MyCollection { MyCollection(Vec::new()) } fn add(&mut self, elem: i32) { self.0.push(elem); } } impl IntoIterator for MyCollection { type Item = i32; type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } fn main() { let mut c = MyCollection::new(); c.add(0); c.add(1); c.add(2); for (i, n) in c.into_iter().enumerate() { assert_eq!(i as i32, n); } }