Hey there. I'm trying to "merge" two Vec
's returned from a database, and feel there must be a more elegant solution. I have two Vec<(Uuid, Vec<&str>)>
variables, for example:
// let uuid_1 = uuid::Uuid::now_v7();
// let uuid_2 ...
let a = vec![(uuid_1, vec!["a", "b", "c"]), (uuid_2, vec!["a", "b", "c", "d", "e"])];
let b = vec![(uuid_3, vec!["a", "b"]]), (uuid_1, vec!["c","d", "e", "f"])];
I wish to combine the inner tuple Vec<&str>
(element 1) of any intersecting element within a
or b
, for the result:
/// let r = combine(a, b);
assert!(
// It is acceptable for the inner `Vec<&str>` to have duplicate variables, as is the case for `"c"` of `uuid_1`
r ==
vec![(uuid_1, vec!["a", "b", "c", "c", "d", "e", "f"]), (uuid_2, vec!["a", "b", "c", "d", "e"]), (uuid_3, vec!["a", "b"])]
// It is also acceptable for the inner `Vec<&str>` to remove duplicates
|| r ==
vec![(uuid_1, vec!["a", "b", "c", "d", "e", "f"]), (uuid_2, vec!["a", "b", "c", "d", "e"]), (uuid_3, vec!["a", "b"])]
);
The only approaches I can think of are to either iterate vector b
for each iteration of vector a
(or vice-versa). Somewhat similar to:
let mut r == vec![];
'outer for x in a {
for y in b {
if x[0] == y[0] {
// Something along the lines of: remove from `b`, chain x[1] and y[1], push to `r` and continue 'outer;
}
}
}
Or, what I expect to be a worse approach, instantiate another struct from these Vec
's that can more conveniently compare Uuid
's, e.g. a HashMap
.
Is there a more elegant approach to what I'm trying to do, one that preferably removed the need to iterate one Vec
for every iteration of the other(like the example above)? I'm especially interested in any iterator methods that could make the code much more elegant, and remove for
loops altogether.