In the event you might like to apply this approach to any number of entries, there are a couple of ways to go about it depending on how you imagine the computation.
// the collection of any arbitrary length
let collection: Vec<(i32, Func)> = vec![(10, two), (20, one), (30, |x| x + 7)];
// using fold
let new_collection = collection.iter().fold(Vec::new(), |mut acc, pair| {
acc.push(pair.1(pair.0));
acc
});
// using map
let new_collection: Vec<i32> = collection.iter().map(|pair| pair.1(pair.0)).collect();
// fold can build anything that is "traversable" aka can be iterated over...
let new_anything = collection.iter().fold(String::new(), |mut acc, pair| {
acc.push_str(&pair.1(pair.0).to_string());
acc
});
// a capacity that is abstracted with the FromIterator trait
#[derive(Debug)]
struct MyType(String);
impl<'a> std::iter::FromIterator<&'a (i32, Func)> for MyType {
fn from_iter<I: IntoIterator<Item = &'a (i32, Func)>>(iter: I) -> Self {
//
// use an adaptor (iter -> iter) that returns an iterator where
// Item: String ... an iterator that can be used to construct a String
// (i.e., where String implements FromIterator)
//
MyType(String::from_iter(
iter.into_iter().map(|pair| pair.1(pair.0).to_string()),
))
}
}
let iter = collection.iter();
let new_my_type = MyType::from_iter(iter);