I have a nested Vec
structure where I need to convert the deepest Vec
into a tuple. Consider the following example:
fn main() {
let input = vec![vec![vec![vec![1.0, 2.0]]]];
let output: Vec<Vec<Vec<(f64, f64)>>> = input
.iter()
.map(|r1| {
r1.iter()
.map(|r2| r2.iter().map(|r3| (r3[0], r3[1])).collect())
.collect()
})
.collect();
}
I make the assumption that there are at least two values in the deepest vector, but if there aren't, then this code will fail. I'd like to have main
return an error to indicate this, but because the indexing is in the iterator chain, I cannot simply use ?
or return an error.
I'd like to not turn this whole thing into for loops. Assuming this, what are my options and what is idiomatic?