Q:

Rust program to compare HashSets using equal to (==) operator

belongs to collection: Rust HashSet Programs

0

In this program, we will create two HashSets to store integer items, and then we will compare HashSets using the equal to "==" operator and print the appropriate message.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to compare HashSets using equal to "==" operator is given below. The given program is compiled and executed successfully.

// Rust program to compare HashSets 
// using equal to "==" operator

use std::collections::HashSet;

fn main() {
    let set1: HashSet<_> = [10, 15, 30, 20,12].iter().cloned().collect();
    let set2: HashSet<_> = [10, 15, 20,40].iter().cloned().collect();
    let set3: HashSet<_> = [10, 15, 20,40].iter().cloned().collect();
    
    if(set1==set2)
    {
        println!("Set1 and Set2 contains same elements");
    }
    else
    {
        println!("Set1 and Set2 contains different elements");
    }
    
    if(set2==set3)
    {
        println!("Set2 and Set3 contains same elements");
    }
    else
    {
        println!("Set2 and Set3 contains different elements");
    }
}

Output:

Set1 and Set2 contains different elements
Set2 and Set3 contains same elements

Explanation:

Here we created two HashSets to store integer elements. Then we compared two HashSets using the equal to "==" operator.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust program to get the capacity of HashSet... >>
<< Rust program to find the Union of two HashSets...