How to Compute the Difference Between Two Sets in Rust

Nilesh Katuwal Feb 02, 2024
  1. Hash Set in Rust
  2. Compute the Difference Between Two Sets in Rust
How to Compute the Difference Between Two Sets in Rust

This article will discuss the built-in function to compute the difference between two sets.

Hash Set in Rust

A hash set is implemented as a HashMap where the value is (). As with the type, elements of a HashSet must implement the Eq and Hash traits.

Typically, this can be accomplished with #[derive(PartialEq, Eq, hash)]. However, if you implement these on your own, the following characteristics must hold.

Syntax:

let mut HashSet_name= HashSet::new();

We need to know if two keys are equal; their hashes should also be equal.

Example:

fn main() {
use std::collections::HashSet;
let mut foods = HashSet::new();
foods.insert("I love to eat".to_string());
foods.insert("Ramen with cheese".to_string());
foods.insert("Ramen with eggs".to_string());
foods.insert("Ramen with seaweed".to_string());

if !foods.contains("Sesame") {
    println!("We have {} foods, but sesame is not the one I wanted.",
             foods.len());
}
foods.remove("Ramen with eggs.");

for food in &foods {
    println!("{food}");
}
}

Output:

We have 4 foods, but sesame is not the one I wanted.
Ramen with cheese
Ramen with seaweed
Ramen with eggs
I love to eat

If you insert a value in the HashSet (i.e., the new value is identical to the existing value and both have the same hash), the new value will replace the old one.

Sets have four fundamental operations (each of the subsequent calls returns an iterator):

  1. union - obtain all the distinct elements from both sets.
  2. difference - retrieve all elements from the first set absent from the second.
  3. intersection - retrieve all unique elements to both sets.
  4. symmetric difference - retrieve all the elements in either set but not both.

Below is an example for finding the difference between sets. We need to know that the difference is not symmetric.

fn main() {
use std::collections::HashSet;
let x = HashSet::from([1, 2, 3]);
let y = HashSet::from([4, 2, 3, 4]);

for z in x.difference(&y) {
    println!("{z}");
}

let diff: HashSet<_> = x.difference(&y).collect();
assert_eq!(diff, [1].iter().collect());

let diff: HashSet<_> = y.difference(&x).collect();
assert_eq!(diff, [4].iter().collect());
}

Output:

1

Compute the Difference Between Two Sets in Rust

This program will build two HashSets to store the integer elements, find the difference between the two sets, and display the result. These two HashSets can store integer elements in the following source code.

The difference between the two sets was then determined using the difference() technique. Then, we printed the outcome.

use std::collections::HashSet;

fn main() {
    let first_set: HashSet<_> = [10, 20, 30, 40].iter().cloned().collect();
    let second_set: HashSet<_> = [10, 20, 30].iter().cloned().collect();

    println!("The difference betwen first_set and second_set is:");
    for d in first_set.difference(&second_set) {
        print!("{} ", d);
    }
}

Output:

The difference betwen first_set and second_set is:
40