Home »
Rust »
Rust Programs
Rust program to find the difference between two HashSets
Rust | HashSet Example: Write a program to find the difference between two HashSets.
Last Updated : October 26, 2021
Problem Statement
In this program, we will create two HashSets to store integer items, and then we will find the difference between both sets and print the result.
Program/Source Code
The source code to find the difference between two HashSets is given below. The given program is compiled and executed successfully.
// Rust program to find the difference
// between two HashSets
use std::collections::HashSet;
fn main() {
let set1: HashSet<_> = [10, 15, 30, 20,12].iter().cloned().collect();
let set2: HashSet<_> = [10, 15, 20].iter().cloned().collect();
println!("Difference (set1-set2):");
for diff in set1.difference(&set2) {
print!("{} ", diff);
}
}
Output
Difference (set1-set2):
30 12
Explanation
Here, we created two HashSets to store integer elements. Then we found the difference between both sets using the difference() method. After that, we printed the result.
Rust HashSet Programs »
Advertisement
Advertisement