Working with HashSet
Track unique values.
Working with HashSet is a free Learn Rust Coding lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a HashSet?
A HashSet stores a collection of unique values. There are no duplicates and no keys, just members.
It is great for answering one question fast: "have I seen this value before?"
Creating a Set
Like HashMap, HashSet lives in std::collections and needs a use import.
Create an empty one with HashSet::new() and declare it mut to add items.
use std::collections::HashSet;
fn main() {
let mut s: HashSet<i32> = HashSet::new();
s.insert(1);
s.insert(2);
println!("size: {}", s.len());
}Inserting and Duplicates
The insert method returns a bool: true if the value was new, false if it was already present.
Inserting a duplicate does nothing, so the set stays unique automatically.
use std::collections::HashSet;
fn main() {
let mut s = HashSet::new();
println!("{}", s.insert(5));
println!("{}", s.insert(5));
println!("len = {}", s.len());
}Membership Tests
Use contains to check whether a value is in the set. It returns a bool.
This is the most common reason to reach for a set: fast, clear lookups.
use std::collections::HashSet;
fn main() {
let mut s = HashSet::new();
s.insert("red");
s.insert("blue");
println!("has red? {}", s.contains("red"));
println!("has green? {}", s.contains("green"));
}Removing Members
The remove method takes a value and returns true if it was present and removed.
Removing something that is not there simply returns false.
use std::collections::HashSet;
fn main() {
let mut s = HashSet::new();
s.insert(7);
println!("{}", s.remove(&7));
println!("{}", s.remove(&7));
}Building From an Iterator
You can collect any iterator of values directly into a HashSet.
This is a quick way to deduplicate: feed in a list with repeats and the set keeps each value once.
use std::collections::HashSet;
fn main() {
let nums = [1, 2, 2, 3, 3, 3];
let unique: HashSet<i32> = nums.iter().copied().collect();
println!("distinct: {}", unique.len());
}Iterating a Set
Loop over a set with a for loop to visit each member.
As with HashMap, the iteration order is not guaranteed and may differ each run.
use std::collections::HashSet;
fn main() {
let s: HashSet<i32> = [10, 20, 30].into_iter().collect();
let mut total = 0;
for v in &s {
total += v;
}
println!("sum = {}", total);
}Union
The union method gives every value that is in either set.
It returns an iterator, so collect it into a new set or loop over it.
use std::collections::HashSet;
fn main() {
let a: HashSet<i32> = [1, 2, 3].into_iter().collect();
let b: HashSet<i32> = [3, 4].into_iter().collect();
let u: HashSet<i32> = a.union(&b).copied().collect();
println!("union size: {}", u.len());
}Intersection
The intersection method yields values found in both sets.
It is perfect for finding common elements, like shared tags or mutual friends.
use std::collections::HashSet;
fn main() {
let a: HashSet<i32> = [1, 2, 3].into_iter().collect();
let b: HashSet<i32> = [2, 3, 4].into_iter().collect();
let common: Vec<i32> = a.intersection(&b).copied().collect();
println!("common count: {}", common.len());
}Difference
The difference method yields values in the first set but not in the second.
Swap the order to get the opposite difference. There is also symmetric_difference for items in exactly one set.
use std::collections::HashSet;
fn main() {
let a: HashSet<i32> = [1, 2, 3].into_iter().collect();
let b: HashSet<i32> = [2].into_iter().collect();
let only_a: Vec<i32> = a.difference(&b).copied().collect();
println!("only in a: {}", only_a.len());
}Subset and Disjoint
Use is_subset to check if every member of one set is in another.
Use is_disjoint to test that two sets share no values at all. Both return a plain bool.
use std::collections::HashSet;
fn main() {
let a: HashSet<i32> = [1, 2].into_iter().collect();
let b: HashSet<i32> = [1, 2, 3].into_iter().collect();
println!("a subset of b? {}", a.is_subset(&b));
}Quick Check
You insert the same value twice into a HashSet.
Recap
A HashSet holds unique values with fast contains checks.
You inserted, removed, deduplicated via collect, and combined sets with union, intersection, and difference.
Frequently asked questions
Is the “Working with HashSet” lesson free?
Yes — the full text of “Working with HashSet” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Working with HashSet”?
Track unique values. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Working with HashSet” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building a HashMap
- Entry API and Defaults
- Working with HashSet
- Counting and Grouping