Guest User

Untitled

a guest
Apr 26th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct MyCollection(Vec<i32>);
  3.  
  4. impl MyCollection {
  5. fn new() -> MyCollection {
  6. MyCollection(Vec::new())
  7. }
  8.  
  9. fn add(&mut self, elem: i32) {
  10. self.0.push(elem);
  11. }
  12.  
  13. fn my_filter(&self) -> impl Iterator<Item=&i32> {
  14. self.0.iter().filter(|&n| *n > 0)
  15. }
  16.  
  17. fn my_map(&self) -> impl Iterator<Item=i32> {
  18. self.0.iter().map(|n| n * 2)
  19. }
  20. }
  21.  
  22. fn main() {
  23. let mut c = MyCollection::new();
  24.  
  25. c.add(0);
  26. c.add(1);
  27. c.add(2);
  28. c.add(3);
  29. c.add(4);
  30. c.add(5);
  31.  
  32. for n in c.my_filter().map(|&n| n * 2) {
  33. println!("{}", n);
  34. }
  35.  
  36. for n in c.my_map().filter(|&n| n > 0) {
  37. println!("{}", n);
  38. }
  39. }
Add Comment
Please, Sign In to add comment