3

The usual way to declare a HashMap in a Rust struct is as follows:

struct MyStruct {
  query: HashMap<String, String>,
  counter: u32,
}

How would I write the above code if I do not know what the HashMap would contain beforehand? I have tried the below code without success.

struct MyStruct {
  query: HashMap<K, V>,
  counter: u32,
}

1 Answer 1

6

You will need to add your generics to your struct declaration as well:

struct MyStruct<K,V> {
  query: HashMap<K, V>,
  counter: u32,
}

Have a look at Rust Book/Generic Data Types

Not the answer you're looking for? Browse other questions tagged or ask your own question.