环境
- Time 2022-03-24
- Rust 1.59.0
示例
new
fn main() {
let map: HashMap<&str, i32> = HashMap::new();
println!("{:?}", map.capacity());
}
with_capacity
fn main() {
let map: HashMap<&str, i32> = HashMap::with_capacity(4);
println!("{:?}", map.capacity());
}
with_hasher
fn main() {
let s = RandomState::new();
let map: HashMap<&str, i32> = HashMap::with_hasher(s);
println!("{:?}", map.capacity());
}
with_capacity_and_hasher
fn main() {
let s = RandomState::new();
let map: HashMap<&str, i32> = HashMap::with_capacity_and_hasher(4, s);
println!("{:?}", map.capacity());
}
capacity
fn main() {
let map: HashMap<&str, i32> = HashMap::with_capacity(4);
println!("{:?}", map.capacity());
}
keys
fn main() {
let map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
map.keys().for_each(|e| println!("{e:?}"));
}
into_keys
fn main() {
let map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
map.into_keys().for_each(|e| println!("{e:?}"));
}
values
fn main() {
let map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
map.values().for_each(|e| println!("{e:?}"));
}
values_mut
fn main() {
let mut map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
map.values_mut().for_each(|e| println!("{e:?}"));
}
总结
了解了哈希映射中相关的一些方法。