【Rust】哈希映射(一)

环境

  • 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:?}"));
}

总结

了解了哈希映射中相关的一些方法。

附录

posted @   jiangbo4444  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-05-29 JavaWeb:HttpSession
点击右上角即可分享
微信分享提示