Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Box<> with HashMap<> in Rust

Tags:

hashmap

rust

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.

like image 482
Giuseppe Avatar asked Oct 27 '25 10:10

Giuseppe


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

like image 153
Ivan C Avatar answered Oct 30 '25 07:10

Ivan C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!