Rust Check Subset Function
Write a Rust function that takes two sets as input and returns true if the first set is a subset of the second set, otherwise returns false.
Sample Solution:
Rust Code:
Output:
Is set1 a subset of set2? true
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 is_subset(set1: &HashSet<i32>, set2: &HashSet<i32>) -> bool { ... }: This line defines a function named is_subset that takes two sets (HashSet<i32>) as input references and returns a boolean value indicating whether the first set is a subset of the second set.
- The "is_subset()" function iterates over each element in the first set ('set1') and checks if it is present in the second set ('set2'). If any element of 'set1' is not found in 'set2', the function returns 'false'. Otherwise, it returns 'true' after checking all elements.
- The "main()" function serves as the entry point of the program. It creates two sample sets ('set1' and 'set2') and calls the "is_subset()" function to check if 'set1' is a subset of 'set2'. Finally, it prints the result.
Rust Code Editor:
Previous: Rust Set Symmetric Difference Function.
Next: Rust Check Superset Function.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics