跳到主要内容

Rust哈希集合

提示
  1. HashSet 基本概念:Rust 中的 HashSet 是一种无重复值的集合数据结构,用于存储唯一的元素。
  2. 创建和操作 HashSet:通过 HashSet::new() 创建 HashSet;支持 insert() 添加值、contains() 检查值、remove() 移除值和使用 for 循环遍历 HashSet。
  3. 集合操作方法:HashSet 提供了 union()intersection()difference()symmetric_difference() 方法来执行集合的并集、交集、差集和对称差集操作。

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

// 从 Rust 标准集合库中导入 HashSet
use std::collections::HashSet;

fn main() {
// 创建一个新的 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();

// 向 hashset 中插入元素
colors.insert("Red");
colors.insert("Yellow");

这里,我们在绑定到变量 colorsHashSet 中插入了两个值。

注意: 由于 mut 变量声明,才可能向 hashset 添加新的值。

示例:向 HashSet 添加值

use std::collections::HashSet;

fn main() {
let mut colors: HashSet<&str> = HashSet::new();

// 向 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);

// 在 HashSet 中检查一个值
if colors.contains("Red") {
println!("我们的 HashSet 中有颜色 \"Red\"。")
}
}

输出 colors = Yellow 我们的 HashSet 中包含颜色 "Red"。


在上面的例子中,我们使用了 `colors.contains("Red")` 作为 `if` 语句的条件。

这里,元素 `Red` 存在于哈希集中,所以条件为真。因此,我们得到了期望的输出。

## 3. 在 Rust 中从 HashSet 中移除值

我们可以使用 `remove()` 方法从哈希集中移除指定的元素。例如,

```rust exec
use std::collections::HashSet;

fn main() {
let mut colors: HashSet<&str> = HashSet::new();

colors.insert("Red");
colors.insert("Yellow");
colors.insert("Green");

println!("移除操作前的 colors = {:?}", colors);

// 从 HashSet 中移除值
colors.remove("Yellow");

println!("移除操作后的 colors = {:?}", colors);
}

输出

移除操作前的 colors = {"Yellow", "Green", "Red"}
移除操作后的 colors = {"Green", "Red"}

在上述例子中,我们使用了

colors.remove("Yellow");

来从哈希集中移除元素 Yellow

4. 在 Rust 中遍历 HashSet 的值

我们可以使用 Rust for 循环 来遍历哈希集的值。例如,

use std::collections::HashSet;

fn main() {
let mut colors: HashSet<&str> = HashSet::new();

colors.insert("Red");
colors.insert("Yellow");
colors.insert("Green");

// 遍历一个哈希集
for color in colors {
// 打印哈希集中的每个值
println!("{}", color);
}
}

输出

Red
Green
Yellow

这里,我们遍历名为 colors 的哈希集,并打印出每个元素。

Rust 中带有默认值的 HashSet

我们还可以使用 from() 方法在创建时为哈希集添加默认值。例如,

use std::collections::HashSet;

fn main() {
// 使用 from() 方法创建带有默认值集合的 HashSet
let numbers = HashSet::from([2, 7, 8, 10]);

println!("numbers = {:?}", numbers);
}

输出

numbers = {8, 2, 7, 10}

这里,我们使用 HashSet::from() 方法创建了一个带有默认值的哈希集,并将其打印到屏幕上。

Rust HashSet 的其他方法

除了基本方法之外,这里还有一些更常用的 HashSet 方法。

方法描述
len()返回哈希集的长度
is_empty()检查哈希集是否为空
clear()从哈希集中移除所有元素
drain()返回哈希集的所有元素的迭代器,并清空哈希集

集合操作

HashSet 模块还提供了用于执行不同集合操作的各种方法。

1. 两个集合的并集

我们可以使用 union() 方法来找到两个集合的并集。例如,

use std::collections::HashSet;

fn main() {
let hashset1 = HashSet::from([2, 7, 8]);
let hashset2 = HashSet::from([1, 2, 7]);
// 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() 方法来找到两个集合的并集:hashset1hashset2

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]);

// 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]);

// 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]);

// 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}