Q:

Rust program to find the Symmetric difference between two HashSets

belongs to collection: Rust HashSet Programs

0

In this program, we will create two HashSets to store integer items, and then we will find the Symmetric difference between both sets and print the result.

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 find the Symmetric difference between two HashSets is given below. The given program is compiled and executed successfully.

// Rust program to find the Symmetric 
// 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,40].iter().cloned().collect();

    println!("Symmetric Difference:");
    for diff in set1.symmetric_difference(&set2) {
        print!("{} ", diff); 
    }
}

Output:

Symmetric Difference:
12 30 40

Explanation:

Here, we created two HashSets to store integer elements. Then we found the symmetric difference between both sets using the symmetric_difference() method. After that, we printed the result.

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

total answers (1)

Rust program to find the intersection of two HashS... >>
<< Rust program to find the difference between two Ha...