I created a small example that runs in the Rust Playground:
#[derive(Clone)]
pub struct Person {
pub firstname: Vec<u8>,
pub surname: Vec<u8>,
}
fn main() {
let p0 = Person {
firstname: vec![0u8; 5],
surname: vec![1u8; 5],
};
let p1 = Person {
firstname: vec![2u8; 7],
surname: vec![3u8; 2],
};
let p2 = Person {
firstname: vec![4u8; 8],
surname: vec![5u8; 8],
};
let p3 = Person {
firstname: vec![6u8; 3],
surname: vec![7u8; 1],
};
let people = [p0, p1, p2, p3];
for i in 0..people.len() {
if i + 1 < people.len() {
println!(
"{:?}",
(people[i].firstname.clone(), people[i + 1].surname.clone())
)
}
}
}
Given the array people, I want to iterate its elements and collect tuples of the first name of the person at index i and the surname of the person at index i+1. This simple for loop does the job, but let's assume that instead of println I would like to pass such tuple into some function f. I can easily do this in this for loop, but I would like to learn whether I can implement that using an iterator iter() (and later apply collect() or fold functions if needed) instead of using the for loop?