Using Box<> with HashMap<> in Rust

Viewed 1144

I need to create a large HashMap in Rust, which is why I thought of using the Box to use the heap memory.
My question is about what is the best way to keep this data, certainly I only thought of two possible ways (anticipating that I am not so experienced with Rust).


fn main() {
  let hashmap = Box<HashMap<u64, DataStruct>>;
  ...
}

OR

fn main() {
  let hashmap = HashMap<u64, Box<DataStruct>>;
  ...
}

What is the best way to handle such a thing?
Thanks a lot.

1 Answers

HashMap already stores its data on the heap, you don't need to box your values.

Just like vectors, hash maps store their data on the heap. This HashMap has keys of type String and values of type i32. Like vectors, hash maps are homogeneous: all of the keys must have the same type, and all of the values must have the same type.

Source

Related