HashSets in Rust

Introduction to HashSets in Rust

Hello! Today, we are going to explore HashSets, a powerful data structure in Rust that belongs to the collections module. HashSets provide us with an efficient way to store and manage unique items. As we delve into this lesson, you'll learn how to create, manipulate, and leverage the power of HashSets to solve common programming problems.

Rust's HashSet is an unordered collection that uses a hash function to manage its elements, ensuring that each element is unique. This makes HashSets incredibly useful for tasks where you need to check for membership, eliminate duplicates, or perform set operations. Let's get started!

Creating a HashSet

In Rust, creating a HashSet involves using the HashSet struct from the std::collections module. You can either create an empty HashSet and then add elements to it or create a Hashset with default values.

Here's how to create a HashSet:

use std::collections::HashSet;

fn main() {
    // Create an empty HashSet
    let mut empty_set: HashSet<i32> = HashSet::new();
    let mut set = HashSet::from([1,2,3,4]);
}
  • We first import the HashSet struct from the std::collections module.
  • We then create an empty HashSet named empty_set, which can store i32 values.
  • We then create a HashSet named set, which already contains some values.

Adding and Removing Elements

Once you have a HashSet, you can add or remove elements using the insert and remove methods.

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

    // Add values to HashSet
    hashset.insert(1);
    hashset.insert(2);
    hashset.insert(3);

    // Remove values from HashSet
    hashset.remove(&2);
}
  • The insert method adds a value to the HashSet. If the value already exists, it will not be added again.
  • The remove method removes a value from the HashSet, if it exists. The value passed into remove must always be a reference.

Checking Membership and Other Properties

One of the key advantages of using a HashSet is the ability to quickly check if an item exists within the set. You can also check the length of the HashSet and whether it's empty.

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

    // Check membership in HashSet
    let has_one = hashset.contains(&1);
    let has_two = hashset.contains(&2);
    println!("HashSet has 1: {}, has 2: {}", has_one, has_two); // Prints: "HashSet has 1: true, has 2: false"

    // len() - get the number of elements
    let length = hashset.len();
    println!("Length of HashSet: {}", length); // Prints: "Length of HashSet: 2"

    // is_empty() - check if the set is empty
    let is_empty = hashset.is_empty();
    println!("Is HashSet empty: {}", is_empty); // Prints: "Is HashSet empty: false"
}
  • The contains method checks whether a value exists in the HashSet and returns a boolean. contains always expects a reference as an input.
  • The len method returns the number of elements in the HashSet.
  • The is_empty method checks if the HashSet is empty.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal