Rust HashMap 数据结构允许我们将数据存储在键值对中。以下是 HashMap 的一些特性:
- 每个值都与一个对应的键相关联。
- 键是唯一的,而值可以重复。
- 可以使用对应的键来访问值。
在 Rust 中创建 HashMap
HashMap 是 Rust 标准集合库的一部分,因此为了使用它,我们必须在程序中包含 HashMap
模块。
use std::collections::HashMap;
我们可以使用 use
声明来导入 HashMap
模块。它应该位于程序顶部。
现在,我们可以使用 HashMap
模块中的 new()
方法创建一个 hashmap。例如,
let mut info: HashMap<i32, String> = HashMap::new();
这里,
let mut info
- 声明一个可变变量info
HashMap<i32, String>
- HashMap 的类型,其中键是整数,值是字符串HashMap::new()
- 创建一个新的 HashMap
示例:创建 HashMap
// import HashMap from Rust standard collections library
use std::collections::HashMap;
fn main() {
// create a new HashMap
let mut info: HashMap<i32, String> = HashMap::new();
println!("HashMap = {:?}", info);
}
输出
HashMap = {}
在这里,我们创建一个空的 HashMap 并将其打印到屏幕上。
注意:我们在 println!
宏中使用 :?
来打印 HashMap。
Rust 中的 HashMap 操作
HashMap
模块提供了各种方法来在 hashmap 中执行基本操作。
- 添加元素
- 访问值
- 移除元素
- 更改元素
1. 在 Rust 中向 HashMap 添加元素
我们可以使用 insert()
向 hashmap 添加一个元素(键值对)。例如,
let mut fruits: HashMap<i32, String> = HashMap::new();
// insert elements to hashmap
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
在这里,我们在绑定到变量 fruits
的 HashMap
中插入了两个键值对。这里的 String::from()
方法创建了一个 String
类型的值。
注意:由于 mut
变量声明,才能向 HashMap 添加新的键值对。
示例:向 HashMap 添加元素
use std::collections::HashMap;
fn main() {
let mut fruits: HashMap<i32, String> = HashMap::new();
// add key-value in a hashmap
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
println!("fruits = {:?}", fruits);
}
输出
fruits = {1: "Apple", 2: "Banana"}
2. 在 Rust 中访问 HashMap 中的值
我们可以使用 get()
从给定的 hashmap 中访问值。例如,
let mut fruits: HashMap<i32, String> = HashMap::new();
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
let first_fruit = fruits.get(&1);
在这里,我们使用键 &1
和 get()
方法从 hashmap 中获取一个值。
我们将 ampersand(&
)和键(&1
)作为参数,因为 get()
返回的是值的引用,而不是 HashMap 中的实际值。
示例:访问 HashMap 中的值
use std::collections::HashMap;
fn main() {
let mut fruits: HashMap<i32, String> = HashMap::new();
// insert elements in a hashmap
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
// access values in a hashmap
let first_fruit = fruits.get(&1);
let second_fruit = fruits.get(&2);
let third_fruit = fruits.get(&3);
println!("first fruit = {:?}", first_fruit);
println!("second fruit = {:?}", second_fruit);
println!("third fruit = {:?}", third_fruit);
}
输出
first fruit = Some("Apple") second fruit = Some("Banana") third fruit = None
请注意,我们将 ampersand(&
)和键(&1
, &2
)作为参数传递给 get()
方法。
let first_fruit = fruits.get(&1);
let second_fruit = fruits.get(&2);
get()
方法的输出是一个 Option
枚举,这意味着如果作为参数传递的键匹配,它将返回 Some
值,如果不匹配,它将返回 None
。
在上面的示例中,let third_fruit = fruits.get(&3)
返回 None
,因为键 &3
在 hashmap 中不匹配任何内容。
3. 从 Rust HashMap 中移除元素
我们可以通过向 remove()
方法提供一个键来从 hashmap 中移除元素。例如,
let mut fruits: HashMap<i32, String> = HashMap::new();
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
fruits.remove(&1);
在这里,我们使用键和 remove()
方法从 hashmap 中移除一个值。
示例:移除 HashMap 中的元素
use std::collections::HashMap;
fn main() {
let mut fruits: HashMap<i32, String> = HashMap::new();
// insert values in a hashmap
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
println!("fruits before remove operation = {:?}", fruits);
// remove value in a hashmap
fruits.remove(&1);
println!("fruits after remove operation = {:?}", fruits);
}
输出
fruits before remove operation = {1: "Apple", 2: "Banana"} fruits after remove operation = {2: "Banana"}
在这里,我们使用 remove()
方法移除键为 &1
的 HashMap 中的一个元素。
4. 更改 Rust HashMap 中的元素
我们可以使用 insert()
方法更改/更新 hashmap 中的元素。例如,
let mut fruits: HashMap<i32, String> = HashMap::new();
// insert values in the hashmap
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
// update the value of the element with key 1
fruits.insert(1, String::from("Mango"));
在这里,最后一个 insert 表达式更新了键为 1 的元素的初始值。
示例:更改 HashMap 中的元素
use std::collections::HashMap;
fn main() {
let mut fruits: HashMap<i32, String> = HashMap::new();
// insert values in a hashmap
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
println!("Before update = {:?}", fruits);
// change value of hashmap with key of 1
fruits.insert(1, String::from("Mango"));
println!("After update = {:?}", fruits);
}
输出
Before update = {1: "Apple", 2: "Banana"} After update = {1: "Mango", 2: "Banana"}
Rust HashMap 的其他方法
除了基本方法之外,这里还有一些常用的 HashMap 方法。
方法 | 描述 |
---|---|
len() |
返回 HashMap 的长度。 |
contains_key() |
检查指定键是否存在对应的值。 |
iter() |
返回 HashMap 条目的迭代器。 |
values() |
返回 HashMap 值的迭代器。 |
keys() |
返回 HashMap 键的迭代器。 |
clone() |
创建并返回 HashMap 的副本。 |
示例:Rust HashMap 方法
use std::collections::HashMap;
fn main() {
let mut fruits: HashMap<i32, String> = HashMap::new();
fruits.insert(1, String::from("Apple"));
fruits.insert(2, String::from("Banana"));
// loop and print values of hashmap using values() method
for fruit in fruits.values() {
println!("{}", fruit)
}
// print the length of hashmap using len() method
println!("Length of fruits = {}", fruits.len());
}
输出
Apple Banana Length of fruits = 2
在这里,我们使用 HashMap 的 values()
方法来遍历其值,并使用 hashmap 的 len()
方法来查找其长度。