HashSet in Rust

A HashSet in Rust is a collection that holds a unique value inside it and does not permit the entry of any duplicate values. HashSet is different from HashMap in the sense, that in HashSet, there is no key-value pair and the value or the data inside it, should be directly accessed.

The HashSet structure is defined inside the std::collections of the Rust. And so to use the HashSet structure, this collection should be imported explicitly at the beginning of any program, which is using HashSet.

The syntax for importing the HashSet collection:

use std::collections:HashMap;

A HashSet can be created directly by using the static method new(). And then we can initialize the HashSet.

The syntax for creating a HashSet:

let mut HashSet_name= HashSet::new();

A simple program to insert data and display the content in the HashMap :

use std::collections::HashSet;
fn main() {
  let mut department = HashSet::new();
  department.insert("Computer Science");
  println!("{:?}", department);
}

In the above program, we have imported the std::collections:HashSet explicitly. And inside the main(), function we have declared and created a new HashSet called department, with the help of the static method macro new(). And then using the function insert(), we have directly inserted the data inside the function.

Frequently used Functions in Rust HashSet

There are different functions in the Rust Library which can be used for implementing the HashSet. They are as follows:

  • insert() : This function is used to insert data in the HashSet.
  • len() : This function returns the number of elements in the HashSet, which is the length of the HashSet.
  • get() : This function returns a reference of value if that value is matched with a value present in the HashSet.
  • iter() : This function is used to access all the elements in the HashSet but in arbitrary order.
  • contains : This function returns a value if an elements are found in the HashSet.
  • remove() : This function removes an element from the HashSet.

Program Using the insert()

The function insert() is used to insert data into the HashMap. It may be noted that the function insert(), will not allow any data which is already present in the HashSet. Therefore, there cannot be any duplicacy of the data in the HashMap.

Syntax :

HashSet_name.insert(data)

Program to insert and display data :

use std::collections::HashSet;
fn main() {
   let mut department = HashSet::new();
   department.insert("Computer Science");
   println!("{:?}", department);
}

Program Output :

rust-hashset-insert

Program Using len()

The function len(), is used to retrieve the number of the elements in the HashSet which is the length of the HashMap.

Syntax :

HashSet_name.len()

Program to display the length of a HashMap :

use std::collections::HashSet;
fn main() {
   let mut department = HashSet::new();
   department.insert("CSE");
   department.insert("ME");
   department.insert("IT");
   println!("The length of the HashMap is {}", department.len());
}

In the above program, we have created a HashMap called the department and inserted three values inside it. And then we have printed the length of the function using the function len().

Program Using iter()

The function iter() should be used with the for loop. Using the iter() function we can access the data inside the HashSet.

Syntax :

for variable_name in HashSet_name.iter() {
      //Some code
   }

A program to display all the elements in the HashSet:

use std::collections::HashSet;
fn main() {
  let mut department = HashSet::new();
  department.insert("CSE");
  department.insert("ME");
  department.insert("IT");
  for dept in department.iter() {
    println!("{}", dept);
  }
}

In the above program, we have created a HashSet called department and inserted three different values inside it. And then we have used the function iter() and a loop variable called dept to display all contents of the HashSet.

Program Output :

rust-hashset-iter

In the above output, it can be seen that all the elements of the HashMap is printed.

Program Using get()

The function get() is used to check whether an element is present in the HashSet. The function get() is used along with the match operator. If the value is found with the help of the get() function, then the Some part of the match is executed, otherwise the None(Null) part of the match is executed.

Syntax :

 match HashSet_name.get( & "element") {
   Some(data) => {
     println!("Found, the element is {}", data);
   }
   None => {
     println!("Not Present");
   }
 }

A program to check whether the element is present inside the HashSet.

use std::collections::HashSet;
fn main() {
    let mut department = HashSet::new();
    department.insert("CSE");
    department.insert("ME");
    department.insert("IT");
    match department.get( & "ME") {
        Some(value) => {
            println!("The department {} is present in the HashSet", value);
        }
        None => {
            println!("The element is not present in the HashSet");
        }
    }
}

In the above program, the elements are entered into the HashSet. And using the get() method, the match for the element is searched. If the data is matched, then the Some part will be executed, otherwise, the None part will be executed.

Program Output :

rust-hashset-match

Programs using contains()

The function contains() is used to check whether an element is present in the HashSet. This function contains(), will directly pass the control to execute a statement or a block of a statement, if an element is found in the HashMap.

 if HashSet_name.contains( & "element") {
     //some code
 }

A program to check whether an element is present in the HashSet:

use std::collections::HashSet;
fn main() {
    let mut department = HashSet::new();
    department.insert("CSE");
    department.insert("ME");
    department.insert("IT");
    if department.contains( & "CSE") {
        println!("Element is present in the HashSet");
    }
}

Program Output :

rust-hashset-contains

Program using remove()

The function remove() is used to remove an element from the HashSet.

Syntax :

HashSet_name.remove( & "element");

A program to remove an element from a HashSet:

use std::collections::HashSet;
fn main() {
  let mut department = HashSet::new();
  department.insert("CSE");
  department.insert("ME");
  department.insert("IT");
  department.remove( & "CSE");
  if department.contains( & "CSE") {
    println!("Element is present in the HashSet");
  }
  else {
    println!("The department is not present in the HashSet");
  }
}

In the above program, we have removed and element using - department.remove( & "CSE"); , and then we have tried to check with the help of if statement whether the element CSE, it still inside the HashSet.

Program Output :

rust-hashset-remove

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Subscribe to See Videos

Subscribe to my Youtube channel for new videos : Subscribe Now