Sets (std::set)
Set guarantees that each element occurs at most once. Import with
use std::Set. The API covers creation, insertion, removal, membership tests
and the algebraic set operations — union and intersection.
Create and add
Set::new() creates an empty set. add silently ignores duplicate values.
has tests membership in O(1).
Automatic deduplication of strings and integers; loop duplicate-filter pattern.
// Feature: Set.new / Set.add / Set.has — create and populate a set
// When to use: enforcing uniqueness, fast membership tests.
use std::Set
let s = Set::new()
s.add("a")
s.add("b")
s.add("a") // duplicate: ignored
print(s.len()) // expected: 2
print(s.has("a")) // expected: true
print(s.has("c")) // expected: false
// Set with integers.
let nums = Set::new()
nums.add(1)
nums.add(2)
nums.add(3)
nums.add(2) // dup
print(nums.len()) // expected: 3
// Pattern: filter duplicates in a loop.
let raw = ["foo", "bar", "foo", "baz", "bar", "qux"]
let uniq = Set::new()
for w in raw {
uniq.add(w)
}
print(uniq.len()) // expected: 4
Remove and check size
remove(v) deletes the element — operating on a missing element is safe.
is_empty() is the shorthand for len() == 0.
is_empty before and after insertions; idempotent remove; incremental emptying.
// Feature: Set.remove / Set.len / Set.is_empty
// When to use: clearing entries dynamically, checking for an empty set.
use std::Set
let s = Set.new()
print(s.is_empty()) // expected: true
print(s.len()) // expected: 0
s.add("a")
s.add("b")
s.add("c")
print(s.is_empty()) // expected: false
print(s.len()) // expected: 3
s.remove("b")
print(s.has("b")) // expected: false
print(s.len()) // expected: 2
// remove on a missing key is safe.
s.remove("z")
print(s.len()) // expected: 2
// Empty it out.
s.remove("a")
s.remove("c")
print(s.is_empty()) // expected: true
Union and intersection
union returns a new set with all elements from both; intersect
returns only the common elements. Set::from(array) is the array-based constructor.
Shared tags between two posts via intersect; union to combine two sets.
// Feature: Set.union / Set.intersect — set operations
// When to use: combining tag lists, finding common elements.
use std::Set
let a = Set::from([1, 2, 3])
let b = Set::from([2, 3, 4])
// Union — every element in A or B.
let u = a.union(b)
print(u.len()) // expected: 4
print(u.has(1)) // expected: true
print(u.has(4)) // expected: true
// Intersection — only elements in both A and B.
let i = a.intersect(b)
print(i.len()) // expected: 2
print(i.has(2)) // expected: true
print(i.has(3)) // expected: true
print(i.has(1)) // expected: false
// Practical case: tags shared between two posts.
let tags1 = Set::from(["rust", "zolo", "compilers"])
let tags2 = Set::from(["zolo", "vm", "compilers"])
let common = tags1.intersect(tags2)
print(common.len()) // expected: 2
print(common.has("zolo")) // expected: true
print(common.has("compilers")) // expected: true
Convert to array
Set::from(array) deduplicates on construction; to_array() extracts the elements
back. Composing both is the standard idiom for deduplicating a list.
Set::from + to_array to deduplicate; union + to_array to merge two lists without repetition.
// Feature: Set.from / Set.to_array — converting between Set and array
// When to use: deduplicating an array, then iterating or serializing the result.
use std::Set
// from — creates a set from an array, deduplicating.
let s = Set::from([1, 2, 2, 3, 3, 3, 4])
print(s.len()) // expected: 4
// to_array — extracts the elements as an array.
let arr = s.to_array()
print(arr.len()) // expected: 4
// Classic pattern: deduplicate a list of strings.
let raw = ["foo", "bar", "foo", "baz", "bar"]
let unique = Set::from(raw).to_array()
print(unique.len()) // expected: 3
// Combine union + to_array to merge two lists without duplicates.
let l1 = ["red", "green"]
let l2 = ["green", "blue"]
let merged = Set::from(l1).union(Set::from(l2)).to_array()
print(merged.len()) // expected: 3
Challenge
Given an array of words with repetitions, use Set::from to obtain the unique ones,
convert back to an array with to_array, sort manually (with reduce or sort
if available) and print in order.
See also