Guest User

Untitled

a guest
Mar 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. pub struct FamilyIterator {
  2. offset: usize,
  3. family: Family,
  4. }
  5.  
  6. impl Iterator for FamilyIterator {
  7. type Item = Person;
  8.  
  9. fn next(&mut self) -> Option<Person> {
  10. let result = match self.offset {
  11. 0 => self.family.a,
  12. 1 => self.family.b,
  13. 2 => self.family.c,
  14. _ => return None,
  15. };
  16. self.offset += 1;
  17. Some(result)
  18. }
  19. }
  20.  
  21. impl IntoIterator for Family {
  22. type Item = Person ;
  23. type IntoIter = FamilyIterator;
  24.  
  25. fn into_iter(self) -> Self::IntoIter {
  26. FamilyIterator {
  27. offset: 0,
  28. family: self,
  29. }
  30. }
  31. }
  32.  
  33. pub struct Person {
  34. value: usize,
  35. }
  36.  
  37. pub struct Family {
  38. a: Person,
  39. b: Person,
  40. c: Person,
  41. }
  42.  
  43.  
  44.  
  45. fn main() {
  46. println!("Hello, world!");
  47. let family = Family{a: Person{value: 0}, b: Person{value: 1}, c: Person{value: 2}};
  48.  
  49. for person in family {
  50. println!("Person's value: {}", person.value);
  51. }
  52. }
Add Comment
Please, Sign In to add comment