Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct FamilyIterator {
- offset: usize,
- family: Family,
- }
- impl Iterator for FamilyIterator {
- type Item = Person;
- fn next(&mut self) -> Option<Person> {
- let result = match self.offset {
- 0 => self.family.a,
- 1 => self.family.b,
- 2 => self.family.c,
- _ => return None,
- };
- self.offset += 1;
- Some(result)
- }
- }
- impl IntoIterator for Family {
- type Item = Person ;
- type IntoIter = FamilyIterator;
- fn into_iter(self) -> Self::IntoIter {
- FamilyIterator {
- offset: 0,
- family: self,
- }
- }
- }
- pub struct Person {
- value: usize,
- }
- pub struct Family {
- a: Person,
- b: Person,
- c: Person,
- }
- fn main() {
- println!("Hello, world!");
- let family = Family{a: Person{value: 0}, b: Person{value: 1}, c: Person{value: 2}};
- for person in family {
- println!("Person's value: {}", person.value);
- }
- }
Add Comment
Please, Sign In to add comment