在 Rust 中使用 Box<> 和 HashMap<>
Using Box<> with HashMap<> in Rust
我需要在 Rust 中创建一个大的 HashMap
,这就是为什么我想到使用 Box
来使用堆内存。
我的问题是关于保存这些数据的最佳方式是什么,当然我只想到了两种可能的方式(预计我对 Rust 不是很熟悉)。
fn main() {
let hashmap = Box<HashMap<u64, DataStruct>>;
...
}
或
fn main() {
let hashmap = HashMap<u64, Box<DataStruct>>;
...
}
处理这种事情的最佳方法是什么?
非常感谢。
HashMap 已经将它的数据存储在堆上,你不需要装箱你的值。
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.
我需要在 Rust 中创建一个大的 HashMap
,这就是为什么我想到使用 Box
来使用堆内存。
我的问题是关于保存这些数据的最佳方式是什么,当然我只想到了两种可能的方式(预计我对 Rust 不是很熟悉)。
fn main() {
let hashmap = Box<HashMap<u64, DataStruct>>;
...
}
或
fn main() {
let hashmap = HashMap<u64, Box<DataStruct>>;
...
}
处理这种事情的最佳方法是什么?
非常感谢。
HashMap 已经将它的数据存储在堆上,你不需要装箱你的值。
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.