Guest User

Untitled

a guest
May 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. struct CountingIteratorWrapper<I> {
  2. i: I,
  3. n: usize,
  4. }
  5.  
  6. impl<I> CountingIteratorWrapper<I> {
  7. fn new(i: I) -> Self { Self { i, n: 0 } }
  8. fn so_far(&self) -> usize { self.n }
  9. }
  10.  
  11. impl<I:Iterator> Iterator for CountingIteratorWrapper<I> {
  12. type Item = I::Item;
  13. fn next(&mut self) -> Option<Self::Item> {
  14. let v = self.i.next()?;
  15. self.n += 1;
  16. Some(v)
  17. }
  18. }
  19.  
  20. fn main() {
  21. let mut i = CountingIteratorWrapper::new(0..10);
  22. let v: Vec<_> = i.by_ref().filter(|x| x % 3 == 0).collect();
  23. println!("{:?} {:?}", i.so_far(), v);
  24. }
Add Comment
Please, Sign In to add comment