Rust Tutorials: HashSet<T> 🎯

beginner
9 min

Rust Tutorials: HashSet<T> 🎯

Welcome to our Rust Tutorials! Today, we're diving into the world of HashSet<T>, a powerful and versatile data structure that's an essential part of Rust's standard library. Let's get started! 🚀

What is a HashSet<T>? 📝

A HashSet<T> is a collection of unique values of any type T that uses a hash function for storing and retrieving elements. It's similar to an array, but with a few key differences. Unlike arrays, HashSet<T> automatically handles the addition, removal, and searching of elements for us, making it ideal for working with large sets of data.

Creating a HashSet<T> 💡

To create a HashSet<T>, we first need to import the std::collections module, and then use the HashSet struct. Here's an example of creating an empty HashSet that can store i32 values:

rust
use std::collections::HashSet; fn main() { let mut numbers: HashSet<i32> = HashSet::new(); }

In the example above, we've created an empty HashSet<i32> called numbers and marked it as mutable since we'll be adding elements to it later.

Adding Elements to a HashSet<T> 📝

To add an element to a HashSet<T>, we can use the insert() method. Let's see how we can add numbers to our numbers HashSet<i32>:

rust
use std::collections::HashSet; fn main() { let mut numbers: HashSet<i32> = HashSet::new(); numbers.insert(1); numbers.insert(2); numbers.insert(3); }

Now, our numbers HashSet<i32> contains the numbers 1, 2, and 3. Note that HashSet<T> automatically checks for duplicates and only adds unique values.

Removing Elements from a HashSet<T> 💡

To remove an element from a HashSet<T>, we can use the remove() method. Here's how we can remove the number 1 from our numbers HashSet<i32>:

rust
use std::collections::HashSet; fn main() { let mut numbers: HashSet<i32> = HashSet::new(); numbers.insert(1); numbers.insert(2); numbers.insert(3); numbers.remove(1); }

Now, our numbers HashSet<i32> no longer contains the number 1.

Checking if an Element Exists in a HashSet<T> 📝

To check if an element exists in a HashSet<T>, we can use the contains() method. Here's how we can check if the number 1 exists in our numbers HashSet<i32>:

rust
use std::collections::HashSet; fn main() { let mut numbers: HashSet<i32> = HashSet::new(); numbers.insert(1); numbers.insert(2); numbers.insert(3); println!("Does 1 exist in the set? {}", numbers.contains(&1)); }

In this example, the output will be true since the number 1 exists in our numbers HashSet<i32>.

Iterating over a HashSet<T> 💡

To iterate over the elements in a HashSet<T>, we can use the iter() method. Here's how we can print all the numbers in our numbers HashSet<i32>:

rust
use std::collections::HashSet; fn main() { let mut numbers: HashSet<i32> = HashSet::new(); numbers.insert(1); numbers.insert(2); numbers.insert(3); for number in numbers.iter() { println!("Number: {}", number); } }

In this example, the output will be:

Number: 2 Number: 3

Since the number 1 has been removed from the set.

Quiz Time! 🎯

Quick Quiz
Question 1 of 1

What does a `HashSet<T>` store in Rust?

That's it for today! We've covered the basics of HashSet<T>, but there's still a lot more to learn. Stay tuned for our next lesson where we'll dive deeper into the world of HashSet<T> and explore some advanced examples. Happy coding! 🎉