#hash-set #set-operations #math

bin+lib hashset_ext

Extension for HashSet with intuitive, chainable Python-like set operations

1 unstable release

Uses new Rust 2024

new 0.1.0 May 18, 2025

#899 in Data structures

MIT license

9KB
119 lines

✨ Python-Style Operator Chaining in Rust

This crate introduces familiar and expressive set operations using symbolic operators like |, &, -, and ^, just like in Python’s set behavior. You can elegantly chain operations for unions, intersections, differences, and symmetric differences, combining Rust’s performance with Python-like readability.

📚 Example Comparison

Python:

set1 = {1, 2}
set2 = {2, 3}
set3 = {3, 4}

union_result = set1 | set2 | set3        # {1, 2, 3, 4}
intersection_result = set1 & set2 & set3 # set()
sym_diff_result = set1 ^ set2 ^ set3     # {1, 2, 4}

Rust:

use hashset_ext::HashSetExt;

let set1 = HashSetExt::from(vec![1, 2]);
let set2 = HashSetExt::from(vec![2, 3]);
let set3 = HashSetExt::from(vec![3, 4]);

let union_result = &set1 | &set2 | &set3;        // HashSetExt({1, 2, 3, 4})
let intersection_result = &set1 & &set2 & &set3; // HashSetExt({})
let sym_diff_result = &set1 ^ &set2 ^ &set3;     // HashSetExt({1, 2, 4})

No runtime deps