HashSet 在 Rust 中实现了集合数据结构。就像集合一样,它允许我们存储不重复的值。
在 Rust 中创建 HashSet
Hashset 是 Rust 标准集合库的一部分,因此我们必须在程序中包含 HashSet
模块。
use std::collections::HashSet;
我们已使用 use
声明导入了 HashSet
模块。它应该位于程序的顶部。
现在,我们可以使用 HashSet
模块的 new()
方法来创建 hashset。例如,
let mut color: HashSet<String> = HashSet::new();
这里,
let mut color
- 声明一个可变变量color
HashSet<String>
- hashset 的类型,其中值为String
类型HashSet::new()
- 创建一个新的 hashset
示例:创建 HashSet
// import HashSet from Rust standard collections library
use std::collections::HashSet;
fn main() {
// create a new HashSet
let mut color: HashSet<String> = HashSet::new();
println!("HashSet = {:?}", color);
}
输出
HashSet = {}
在这里,我们创建一个空的 HashSet
并将其打印到屏幕上。
注意:我们在 println!
宏中使用 :?
来打印 hashset。
Rust 中的 HashSet 操作
HashSet
模块提供了各种方法来在 hashset 中执行基本操作。
- 添加值
- 检查值
- 删除值
- 遍历值
1. 在 Rust 的 HashSet 中添加值
我们可以使用 insert()
方法将元素添加到 hashset。例如,
let mut colors: HashSet<&str> = HashSet::new();
// insert elements to hashset
colors.insert("Red");
colors.insert("Yellow");
在这里,我们将两个值插入到绑定到变量 colors
的 HashSet
中。
注意:向 hashset 添加新值仅因为声明了 mut
变量才成为可能。
示例:向 HashSet 添加值
use std::collections::HashSet;
fn main() {
let mut colors: HashSet<&str> = HashSet::new();
// insert values in a HashSet
colors.insert("Red");
colors.insert("Yellow");
colors.insert("Green");
println!("colors = {:?}", colors);
}
输出
colors = {"Green", "Yellow", "Red"}
在这里,输出中的元素顺序不同。这是因为 hashset 不会保留值的插入顺序。
2. 检查值是否在 Rust 的 HashSet 中
我们使用 contains()
方法来检查值是否在 hashset 中。如果指定的元素存在于 hashset 中,则该方法返回 true,否则返回 false。
让我们看一个例子,
use std::collections::HashSet;
fn main() {
let mut colors: HashSet<&str> = HashSet::new();
colors.insert("Red");
colors.insert("Yellow");
println!("colors = {:?}", colors);
// check for a value in a HashSet
if colors.contains("Red") {
println!("We have the color \"Red\" in the HashSet.")
}
}
输出
colors = {"Red", "Yellow"} We have the color "Red" in the HashSet.
在上面的示例中,我们将 colors.contains("Red")
用作 if
语句的条件。
这里,元素 Red
存在于 hashset 中,因此条件为 true。因此,我们得到了期望的输出。
3. 从 Rust 的 HashSet 中删除值
我们可以使用 remove()
方法从 hashset 中删除指定的元素。例如,
use std::collections::HashSet;
fn main() {
let mut colors: HashSet<&str> = HashSet::new();
colors.insert("Red");
colors.insert("Yellow");
colors.insert("Green");
println!("colors before remove operation = {:?}", colors);
// remove value from a HashSet
colors.remove("Yellow");
println!("colors after remove operation = {:?}", colors);
}
输出
colors before remove operation = {"Yellow", "Green", "Red"} colors after remove operation = {"Green", "Red"}
在上面的示例中,我们使用了
colors.remove("Yellow");
以从 hashset 中删除元素 Yellow
。
4. 遍历 Rust HashSet 的值
我们可以使用 Rust for 循环来遍历 hashset 的值。例如,
use std::collections::HashSet;
fn main() {
let mut colors: HashSet<&str> = HashSet::new();
colors.insert("Red");
colors.insert("Yellow");
colors.insert("Green");
// iterate over a hashset
for color in colors {
// print each value in the hashset
println!("{}", color);
}
}
输出
Red Green Yellow
在这里,我们遍历名为 colors 的 hashset 并打印每个元素。
Rust 中的带默认值的 HashSet
创建时,我们也可以使用 from()
方法创建带默认值的 hashset。例如,
use std::collections::HashSet;
fn main() {
// Create HashSet with default set of values using from() method
let numbers = HashSet::from([2, 7, 8, 10]);
println!("numbers = {:?}", numbers);
}
输出
numbers = {8, 2, 7, 10}
在这里,我们使用 HashSet::from()
方法和默认值创建了一个 hashset,并将其打印到屏幕上。
Rust HashSet 的其他方法
除了基本方法外,这里还有一些更常用的 HashSet 方法。
方法 | 描述 |
---|---|
len() |
返回 hashset 的长度 |
is_empty() |
检查 hashset 是否为空 |
clear() |
删除 hashset 中的所有元素 |
drain() |
返回所有元素作为迭代器并清空 hashset |
集合运算
HashSet 模块还提供了用于执行不同集合操作的各种方法。
1. 两个集合的并集
我们可以使用 union()
方法来查找两个集合的并集。例如,
use std::collections::HashSet;
fn main() {
let hashset1 = HashSet::from([2, 7, 8]);
let hashset2 = HashSet::from([1, 2, 7]);
// Union of hashsets
let result: HashSet<_> = hashset1.union(&hashset2).collect();
println!("hashset1 = {:?}", hashset1);
println!("hashset2 = {:?}", hashset2);
println!("union = {:?}", result);
}
输出
hashset1 = {7, 8, 2} hashset2 = {2, 7, 1} union = {2, 7, 8, 1}
在这里,我们使用 union()
方法查找两个集合 hashset1 和 hashset2 之间的并集。
hashset1.union(&hashset2).collect();
union()
方法返回一个迭代器,因此我们使用 collect()
方法来获取实际结果。
注意:我们将 &hashset2
作为参数传递给 union()
方法,因为它接受引用作为参数。
2. 两个集合的交集
我们可以使用 intersection()
方法来查找两个集合之间的交集。例如,
use std::collections::HashSet;
fn main() {
let hashset1 = HashSet::from([2, 7, 8]);
let hashset2 = HashSet::from([1, 2, 7]);
// Intersection of hashsets
let result: HashSet<_> = hashset1.intersection(&hashset2).collect();
println!("hashset1 = {:?}", hashset1);
println!("hashset2 = {:?}", hashset2);
println!("intersection = {:?}", result);
}
输出
hashset1 = {2, 7, 8} hashset2 = {2, 1, 7} intersection = {7, 2}
3. 两个集合之间的差集
我们可以使用 difference()
方法来查找两个集合之间的差集。例如,
use std::collections::HashSet;
fn main() {
let hashset1 = HashSet::from([1, 2, 3, 4]);
let hashset2 = HashSet::from([4, 3, 2]);
// Difference between hashsets
let result: HashSet<_> = hashset1.difference(&hashset2).collect();
println!("hashset1 = {:?}", hashset1);
println!("hashset2 = {:?}", hashset2);
println!("difference = {:?}", result);
}
输出
hashset1 = {3, 4, 1, 2} hashset2 = {2, 4, 3} difference = {1}
4. 两个集合的对称差集
我们可以使用 symmetric_difference()
方法来查找两个集合的对称差集。对称差集返回两个集合中的值,但不包括同时存在于两个集合中的值。
use std::collections::HashSet;
fn main() {
let hashset1 = HashSet::from([2, 7, 8]);
let hashset2 = HashSet::from([1, 2, 7, 9]);
// Symmetric difference of hashsets
let result: HashSet<_> = hashset1.symmetric_difference(&hashset2).collect();
println!("hashset1 = {:?}", hashset1);
println!("hashset2 = {:?}", hashset2);
println!("symmetric difference = {:?}", result);
}
输出
hashset1 = {8, 7, 2} hashset2 = {2, 9, 1, 7} symmetric difference = {8, 9, 1}