我目前正在进行“Rust by Practice”练习。在“Collection Types > HashMap”部分有一个示例:
use std::collections::HashMap;
fn main() {
let mut map: HashMap<i32, i32> = HashMap::with_capacity(100);
map.insert(1, 2);
map.insert(3, 4);
// Indeed ,the capacity of HashMap is not 100, so we can't compare the equality here.
assert!(map.capacity() >= 100);
println!("Capacity #1: {}", map.capacity());
// Shrinks the capacity of the map with a lower limit. It will drop
// down no lower than the supplied limit while maintaining the internal rules
// and possibly leaving some space in accordance …Run Code Online (Sandbox Code Playgroud)