Hi, I'm having some trouble creating traits for some Hashmaps.
This is my code:
type InstrumentOptions = HashMap<InstrumentName,Instrument>;
trait ComponentOptions<K,V> where K: fmt::Display + Clone {
fn keys(&self) -> Keys<K,V>;
fn get_options (&self) -> Vec<K> {
let mut choices : Vec<K> = vec![];
for (i, key) in self.keys().enumerate() {
println!("{}. {}", i+1, key);
choices[0] = key.clone();
}
choices
}
}
impl <InstrumentName: fmt::Display + Clone, Instrument> ComponentOptions<InstrumentName, Instrument> for InstrumentOptions {
fn keys(&self) -> Keys<InstrumentName, Instrument> {
self.keys()
}
}
where InstrumentName is an enumeration, and Instrument is a structure. There will be similar hashmaps of enumeration and structures.
when I try to compile, what I get is...
error[E0308]: mismatched types
--> src\mission.rs:58:9
|
56 | impl <InstrumentName: fmt::Display + Clone, Instrument> ComponentOptions<InstrumentName, Instrument> for InstrumentOptions {
| -------------- this type parameter
57 | fn keys(&self) -> Keys<InstrumentName, Instrument> {
| -------------------------------- expected `std::collections::hash_map::Keys<'_, InstrumentName, Instrument>` because of return type
58 | self.keys()
| ^^^^^^^^^^^ expected type parameter `InstrumentName`, found `InstrumentName`
|
= note: expected struct `std::collections::hash_map::Keys<'_, InstrumentName, Instrument>`
found struct `std::collections::hash_map::Keys<'_, InstrumentName, item::Instrument>`
Instrument is technically in another file, item, so item::Instrument
is technically correct, but InstrumentName is in another file too, and it has no problem wit that.
It is possible that traits are not the right things, but I have some operations that are the same across some similar HashMaps (as you can see in get options), and it feels like it should be the right thing.
Thanks a lot!