I'm trying to invert a HashMap<String, MyEnum> in Rust, and getting an error:
Code:
use std::collections::HashMap;
// Directive added because of this answer, which eliminated a couple of errors:
// https://github.com/rust-lang/rust/issues/22756#issuecomment-75715762
#[derive(Debug, PartialEq, Eq, Hash)]
enum MyEnum {
Value1,
Value2,
}
static MYMAP: HashMap<String, MyEnum> = HashMap::from([
(String::from("value1"), MyEnum::Value1),
(String::from("value1"), MyEnum::Value1),
]);
static MYMAP_INVERTED: HashMap<MyEnum, String> = HashMap::from([
MYMAP.iter().map(|(k, v)| (v, k)).collect()
]);
Error:
error[E0277]: a value of type `(_, _)` cannot be built from an iterator over elements of type `(&MyEnum, &String)`
--> src/main.rs:15:39
|
15 | MYMAP.iter().map(|(k, v)| (v, k)).collect()
| ^^^^^^^ value of type `(_, _)` cannot be built from `std::iter::Iterator<Item=(&MyEnum, &String)>`
|
= help: the trait `FromIterator<(&MyEnum, &String)>` is not implemented for `(_, _)`
note: required by a bound in `collect`
For more information about this error, try `rustc --explain E0277`.
How would you implement FromIterator for a pair?