今天我们来看看Rust中的hashmaps,在 std::collections
crate中可用,是存储键值对的有效数据结构。本文介绍了创建、插入、访问、更新和迭代散列表等基本操作。通过一个计算单词出现次数的实际例子,我们展示了它们在现实世界中的实用性。Hashmaps提供了一种灵活而强大的组织和访问数据的方法,使其成为Rust编程中不可或缺的工具。
Introduction 介绍
Hashmaps store key value pairs and to use a hashing function to know where to put a key/value.
哈希映射存储键值对,并使用哈希函数来知道在哪里放置键/值。
Hashmaps are a part of the std::collections
crate.
Hashmaps是 std::collections
crate的一部分。
Creation 创作
Just like a vector or a string we can use the new()
method for hashmaps.
就像向量或字符串一样,我们可以使用hashmaps的 new()
方法。
use std::collections::HashMap;
fn main(){
let mut my_map = HashMap::new();
}
Insertion 插入
we can insert key-value pa