ADVERTISEMENT
ADVERTISEMENT

Rust program to find the Symmetric difference between two HashSets

Rust | HashSet Example: Write a program to find the Symmetric difference between two HashSets.
Submitted by Nidhi, on October 26, 2021

Problem Solution:

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.

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.

Rust HashSet Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.