use std::iter::Peekable; pub struct Enumerate where I: Iterator, { iter: Peekable, count: usize, } impl Iterator for Enumerate where I: Iterator, { type Item = (usize, bool, ::Item); #[inline] fn next(&mut self) -> Option<(usize, bool, ::Item)> { self.iter.next().map(|a| { let last = self.iter.peek().is_none(); let ret = (self.count, last, a); // Possible undefined overflow. self.count += 1; ret }) } #[inline] fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } #[inline] fn count(self) -> usize { self.iter.count() } } #[inline] pub fn enumerate(iter: I) -> Enumerate where I: Iterator, { Enumerate { iter: iter.peekable(), count: 0, } }