I have to following piece of code that will have to find groups by comparing different vector elements:
fn main() {
let data = vec![
vec!["EG 1", "EG 2", "EG 3"],
vec!["XG 1", "XG 3"],
vec!["SG 1", "SG 6", "SG 8"],
vec!["PS 1", "PS 8"],
];
let result = Vec::<Vec<&str>>::new();
//
// element1.split_whitespace().last() == element2.split_whitespace().last()
//
assert_eq!(
vec![
vec!["EG 1", "XG 1", "PS 1", "SG 1"],
vec!["EG 2"],
vec!["XG 3", "EG 3"],
vec!["SG 6"],
vec!["SG 8", "PS 8"]
],
result
);
}
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7101df9439dd9cdff9f4b36c99a4efc6
As I commented out, I'd like to use the trailing number for comparing the elements:
element1.split_whitespace().last() == element2.split_whitespace().last()
I'm thinking of changing it in the future to use some library like fuzzywuzzy-rs but it's OK to compare as above for testing the algorithm.
I couldn't construct the loop properly and I have two issues that I'd like to point out:
- I thought of removing the compared elements from Vector for prevent adding the same groups to the result. But I'm not sure how can I use
mut
in this case. I'm not sure if it's a good idea either. - I want to utilize the Rust's Vector/Iterator methods effectively (like
retain
,map
etc.) but I'm not sure how.