This code doesn't compile:
use indexmap::IndexMap;
use either::Either;
type Atom = Either<IndexMap<String, u64>, Vec<String>>;
fn flatten(atoms: Vec<Option<Atom>>) -> Vec<String> {
atoms.into_iter()
.filter(Option::is_some)
.map(|atom| match atom.unwrap() {
Either::Left(map) => map.into_keys().into_iter(),
Either::Right(vec) => vec.into_iter(),
})
.flatten()
.collect()
}
9 | .map(|atom| match atom.unwrap() {
| _________________-
10 | | Either::Left(map) => map.into_keys().into_iter(),
| | --------------------------- this is found to be of type `indexmap::map::IntoKeys<String, u64>`
11 | | Either::Right(vec) => vec.into_iter(),
| | ^^^^^^^^^^^^^^^ expected `IntoKeys<String, u64>`, found `IntoIter<String>`
12 | | })
| |_____- `match` arms have incompatible types
|
= note: expected struct `indexmap::map::IntoKeys<String, u64>`
found struct `std::vec::IntoIter<String>`
There's two ways I can solve this that I know of, but none that I am happy with:
- Use
Box+dyn: Dynamic polymorphism overhead - Collect into a
Vecbeforehand before collecting: More efficient to flatten the iterators then collect rather than allocating twovec's then allocating a new one again
Is there a better option than the two?
Either.itertoolsprovide one, or theeithercrate.