Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct CountingIteratorWrapper<I> {
- i: I,
- n: usize,
- }
- impl<I> CountingIteratorWrapper<I> {
- fn new(i: I) -> Self { Self { i, n: 0 } }
- fn so_far(&self) -> usize { self.n }
- }
- impl<I:Iterator> Iterator for CountingIteratorWrapper<I> {
- type Item = I::Item;
- fn next(&mut self) -> Option<Self::Item> {
- let v = self.i.next()?;
- self.n += 1;
- Some(v)
- }
- }
- fn main() {
- let mut i = CountingIteratorWrapper::new(0..10);
- let v: Vec<_> = i.by_ref().filter(|x| x % 3 == 0).collect();
- println!("{:?} {:?}", i.so_far(), v);
- }
Add Comment
Please, Sign In to add comment