Rust Set Intersection Function
Rust Arrays: Exercise-1 with Solution
Write a Rust function that takes two sets as input and returns a new set containing elements that are common to both input sets.
Sample Solution:
Rust Code:
use std::collections::HashSet; // Import the HashSet type from the standard library
// Define a function to find the intersection of two sets
fn find_intersection(set1: &HashSet<i32>, set2: &HashSet<i32>) -> HashSet {
// Create an empty HashSet to store the intersection
let mut intersection_set = HashSet::new(); // New HashSet to store the intersection elements
// Iterate over each element in the first set
for &element in set1 {
// Check if the element is also present in the second set
if set2.contains(&element) {
// If the element is present in both sets, add it to the intersection set
intersection_set.insert(element);
}
}
// Return the intersection set
intersection_set // Return the intersection set containing common elements
}
fn main() {
// Create two sample sets
let set1: HashSet<i32> = [1, 2, 3, 4, 5].iter().cloned().collect(); // First set
let set2: HashSet<i32> = [4, 5, 6, 7, 8].iter().cloned().collect(); // Second set
// Find the intersection of the two sets
let intersection = find_intersection(&set1, &set2);
// Print the intersection set
println!("Intersection set: {:?}", intersection); // Print the intersection set
}
Output:
Intersection set: {5, 4}
Explanation:
Here is a brief explanation of the above Rust code:
- use std::collections::HashSet;: This line imports the "HashSet" type from the standard library, allowing us to use sets in our code.
- fn find_intersection(set1: &HashSet<i32>, set2: &HashSet<i32>) -> HashSet<i32> { ... }: This line defines a function named "find_intersection()" that takes two sets (HashSet<i32>) as input references and returns a new set containing elements that are common to both input sets.
- let mut intersection_set = HashSet::new();: This line creates an empty "HashSet" named 'intersection_set' to store the intersection of the two input sets.
- for &element in set1 { ... }: This line starts a loop to iterate over each element in the first input set (set1).
- if set2.contains(&element) { ... }: This line checks if the current element from 'set1' is also present in the second input set (set2).
- intersection_set.insert(element);: This line inserts the common element into the intersection_set if it is present in both input sets.
- intersection_set: This line returns the 'intersection_set' containing the common elements.
- fn main() { ... }: This line defines the main function, which is the entry point of the Rust program.
- let set1: HashSet<i32> = [1, 2, 3, 4, 5].iter().cloned().collect();: This line creates the first sample set ('set1') with elements 1 to 5.
- let set2: HashSet<i32> = [4, 5, 6, 7, 8].iter().cloned().collect();: This line creates the second sample set ('set2') with elements 4 to 8.
- let intersection = find_intersection(&set1, &set2);: This line calls the find_intersection function with the two sample sets as input and stores the resulting intersection set in the 'intersection' variable.
- println!("Intersection set: {:?}", intersection);: This line prints the intersection set containing common elements from both input sets.
Rust Code Editor:
Previous: Rust Sets Exercises.
Next: Rust Set Union Function.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/rust/collections_and_data_structures/rust-sets-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics