How would you transform a vector of tuples into two vectors of values?
For example:
struct Pair(String,String);
fn main() -> {
let vector = vec![Pair("cool".to_string(),"nice".to_string()),Pair("cool".to_string(),"nice".to_string())];
let (x,y) = // Something
}
Iterator::unzip
to the rescue:
let (x, y): (Vec<_>, Vec<_>) = vector.into_iter().map(|Pair(a, b)| (a, b)).unzip();
1 Like
Sadly this gives the error:
expected struct `Pair`, found tuple
It works for me on the playground. Are you trying to run it with vector
being a Vec<(String, String)>
instead of a Vec<Pair>
? In that case you can just remove the .map(...)
.
Ah yes, that worked, I will mark your answer as the solution. Thanks your your help!
1 Like
system
closed
November 7, 2021, 8:07pm
#6
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.