Pybites Logo Rust Platform

HashSet Basics

Easy +2 pts

In Python, sets provide unique collections with fast membership testing:

tags = {"rust", "python", "coding"} tags.add("rust") # no duplicate print("rust" in tags) # O(1) lookup a = {1, 2, 3} b = {2, 3, 4} print(a & b) # intersection: {2, 3} print(a | b) # union: {1, 2, 3, 4} print(a - b) # difference: {1}

Rust's HashSet<T> works the same way.

Creating and using HashSet

use std::collections::HashSet; let mut tags: HashSet<String> = HashSet::new(); tags.insert("rust".to_string()); …

Login to see the full exercise.