If you're okay with unpacking into three variables of type KmerMap instead of their internal HashMaps you could do:
match x.as_slice() {
[two, three, four] => {
// Do something with two, three, four
}
[..] => {} // Needed to make the patterns exhaustive
}
If you really want the HashMaps themselves you could do something like:
let maps = x.iter().map(|kmer| &kmer.map).collect::<Box<[_]>>();
match maps.as_ref() {
[map_two, map_three, map_four] => {
// Do something with two, three, four
}
[..] => {} // Needed to make the patterns exhaustive
}
Also note in the above that if you care exclusively about the case where x is of length 3, you could use if let instead of match.
Do you mean that your vec has only 3 KmerMaps or that it only contains lots of KmerMaps with 3 different len values ? For the first case you can into_iter and call next three times. For the second case, "just" reduce to a map of collectionsHashMap<usize, Vec<KmerMap>> if the len values are known, you can reduce to a custom struct.
Without something quite clunky like pre-declaring three identifiers at the function-level scope, as far as I'm aware no. You can reduce the scope nesting with if let like so:
if let [map_two, map_three, map_four, ..] = maps.as_ref() {
// Do something with these variables
}
Fundamentally this tension is a result of the pattern being refutable.