rust
14 lines · 1 tab
Marcus Chen
Jan 2026
1 tab
use std::collections::HashSet;
fn main() {
let mut words = HashSet::new();
words.insert("hello");
words.insert("world");
words.insert("hello"); // Duplicate, not added
println!("Unique words: {}", words.len());
if words.contains("hello") {
println!("Found 'hello'");
}
}
1 file · rust
Explain with highlit
HashSet<T> stores unique values with O(1) average-case membership tests. It's backed by a HashMap<T, ()>. I use it for deduplication, membership checks, and set operations (.union(), .intersection()). Common methods: .insert(v) adds, .contains(&v) checks, .remove(&v) deletes. For iteration, .iter() yields &T. Sets are unordered; use BTreeSet if you need ordering. HashSet is great for "have I seen this before?" checks in parsers, dedup in data pipelines, and implementing set-based algorithms. Like HashMap, it uses a randomized hasher for security. The API is simple and efficient for most set use cases.
Related snips
rust
struct Config<'a> {
name: &'a str,
value: &'a str,
}
fn parse_config(line: &str) -> Config {
Lifetime annotations for flexible borrowing in structs
rust
lifetimes
borrowing
by Marcus Chen
1 tab
rust
use crossbeam::channel::unbounded;
use std::thread;
fn main() {
let (tx, rx) = unbounded();
Crossbeam for advanced concurrent data structures
rust
concurrency
lock-free
by Marcus Chen
1 tab
rust
use tracing::{info, instrument};
#[instrument]
fn process_request(user_id: u64) {
info!(user_id, "Processing request");
// Work happens here
tracing for structured logging and distributed tracing
rust
observability
tracing
by Marcus Chen
1 tab
rust
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
Channels (mpsc) for message passing between threads
rust
concurrency
channels
by Marcus Chen
1 tab
rust
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
#[arg(short, long)]
clap for CLI argument parsing with derive macros
rust
cli
clap
by Marcus Chen
1 tab
rust
macro_rules! create_function {
($func_name:ident) => {
fn $func_name() {
println!("Called {}", stringify!($func_name));
}
};
Declarative macros (macro_rules!) for code generation
rust
macros
metaprogramming
by Marcus Chen
1 tab
Share this code
Here's the card — post it anywhere.