Definition of Rust Set
Rust set is a very important data structure of rust programming language and helps in identifying the key present in the set which possess the required element. Sets in rust programming language mostly revolve around the keys not the values as mostly the key associated with the element is enough to retrieve the information about any element. Sets can perform various operations like union, difference, symmetric difference, an intersection on the data available due to which it gets mostly adopted by programmers. Sets mostly comprise of HashSet and BTreeSet which have their own significance in rust.
Syntax:
The syntax flow for Rust Set is as follows :
use std::collections : : HashSet;
let mut var_0 = HashSet : : new ();
var_0.insert(“some_val”);
{
Logic to be implemented
}
Functional calls ;
Where,
HashSet is imported as part of standard library and collections in the rust followed by conventional variable where the provisioning for insertion of elements is done using the new keyword and then the value is created in order to implement the logic and to perform further functional calls from the set comprising of elements.
How set works in Rust?
- Working of set in Rust is used and performed for various operations and activities that make this data structure quite useful in terms of adaptation by programmers.
- Sets in Rust has the unique feature of wrapping the keys and mapping in an appropriate manner which guarantees not to possess duplicate elements.
- Hash set and B-tree Set are part of the sets in rust which is also part of the hashmap whose values are not much important, but keys present play a very crucial role.
- If two keys present are equal then their hashes will also be the same or equal it should not be unique in nature.
- If in a scenario some logic error happens then in that case the eq and the hash values present might get differ and will create a lot of commotion in terms of execution thus it is important to keep in mind the equality trait or hash trait to determine properly while it is present in that set.
- The scenario mentioned so far arises only in case of un-equal code or by following wrong practices at the time of implementation of the code in a rust programming language.
- Also, there might be a case where the hash implementation with respect to key gets differed then in that case it will be a panic that the contents present within it can get hampered and corrupted which in turn will become useless and might need to be dropped off.
- It is possible to create a HashSet with a fixed list of elements from the defined arrays that is initialized and implemented.
- The hashbrown and HashSet present in the standard collection will need to be imported if in case the elements present in the array need to be displayed, performed operations like symmetric, difference, union, and intersection.
- The standard inbuild library comprising of HashSet needs to be imported initially in order to perform any operation related to Sets and keys with associated values otherwise the execution will not be proper and might get distorted thus throwing exceptions and errors.
- Some of the inbuild hash functions like a hash drain and hash brown and many other needs version compatibility as with each version the standard collection might get differed or the library might have deprecated as well.
- Sets can perform operations that are quite likely to get adopted by the programmers to increase and fasten the computational power with operations on sets like union, intersection, difference, and symmetricity.
Examples
Let us discuss examples of Rust Set.
Example #1: This program demonstrates the Rust hash set which demonstrates the union operation by inserting the elements and representing them as shown in the output.
Code:
use std::collections::HashSet;
fn main()
{
let mut x: HashSet<i32> = vec![3i32, 4, 5].into_iter().collect();
let mut y: HashSet<i32> = vec![4i32, 5, 6].into_iter().collect();
assert!(x.insert(6));
assert!(y.contains(&6));
y.insert(7);
println!("X: {:?}", x);
println!("Y: {:?}", y);
println!("Perform_Union_Operation: {:?}", x.union(&y).collect::<Vec<&i32>>());
}
Output:
Example #2: This program demonstrates the hash set in rust where the difference in both the set is considered and is shown in the output below.
Code:
use std::collections::HashSet;
fn main() {
let mut x: HashSet<i32> = vec![3i32, 4, 5].into_iter().collect();
let mut y: HashSet<i32> = vec![4i32, 5, 6].into_iter().collect();
assert!(x.insert(6));
assert!(y.contains(&6));
y.insert(7);
println!("X: {:?}", x);
println!("Y: {:?}", y);
println!("Difference_in_both: {:?}", x.difference(&y).collect::<Vec<&i32>>());
}
Output:
Example #3: This program demonstrates the representation of a fixed set of elements which in this case is fruits from the set of defined arrays as shown in the output.
Code:
use std::collections::HashSet;
fn main() {
let fruits: HashSet<&'static str> =
[ "apple", "kiwi", "Mango", "Guava" ].iter().cloned().collect();
for x_0 in &fruits {
println!("{:?}", x_0);
}
}
Output:
Example #4: This program demonstrates the hash set where the elements present within the hash set of set_08 will get iterated and will get print in an arbitrary order as shown in the output.
Code:
use std::collections::HashSet;
fn main()
{
let mut set_08: HashSet<_> = [8, 9, 10].iter().cloned().collect();
assert!(!set_08.is_empty());
for i_0 in set_08.drain() {
println!("{}", i_0);
}
assert!(set_08.is_empty());
}
Output:
Example #5: This program demonstrates the Rust set which is used for returning the number of elements in the set for measuring and insertion with assertion and equality of the elements.
Code:
use std::collections::HashSet;
fn main()
{
let mut v_l_0 = HashSet::new();
assert_eq!(v_l_0.len(), 0);
v_l_0.insert(1);
assert_eq!(v_l_0.len(), 1);
println!("{:?}", v_l_0);
}
Output:
Conclusion
Rust is a programing language which is used by most programmers after the emergence of C language and sets as a data structure as part of Rust has added a lot more flexibility and adaptability in terms of performing various operation and manipulation. It is very versatile in nature as it helps in making the entire rust programming an interesting language to learn and enhance the skill.
Recommended Articles
This is a guide to Rust Set. Here we discuss the definition, How set works in Rust, and examples with code implementation. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses