These are wrappers around HashSet
and HashMap
which make them a little more convenient to use.
They aren't intended for use in main applications, but were created to make prototyping and writing short programs easier.
The struct EasySet
wraps HashSet
with some useful trait implementations.
```rust use easy_collections::set;
let a = &set!{1, 2, 3}; let b = &set!{2, 3, 4}; asserteq!(a & b, set!{2, 3}); // intersection asserteq!(a | b, set!{1, 2, 3, 4}); // union asserteq!(a ^ b, set!{1, 4}); // symmetric difference asserteq!(a - b, set!{1}); // difference
let c = &set!{1, 2, 3, 4}; assert!(a < c && b < c); // subset assert!(c > a && c > b); // superset assert!(a == a); // equality ```
The struct EasyMap
wraps HashMap
with some useful trait implementations.
```rust use easy_collections::map;
// 42
here is the default value which is returned when no item exists in the map
// The default value is optional.
let map = map!{42; ("foo", 1), ("bar", 10), ("baz", 100)};
asserteq!(map["foo"], 1);
asserteq!(map["bar"], 10);
asserteq!(map["baz"], 100);
asserteq!(map["nope"], 42);
asserteq!(map["nada"], 42);
asserteq!(map["nuttin'"], 42);
// If you want to create a map with just a single value, and no default, use a trailing comma:
let map = map!{("foo", "bar")}; // creates EasyMap<_, (&str, &str)>
with ("foo", "bar")
as the default value
let map = map!{("foo", "bar"),}; // creates EasyMap<&str, &str>
with and map["foo"] == "bar"
```
Also, both EasyMap
and EasySet
deref to their underlying collections, for example:
```rust let easy: EasySet<_> = set!{"foo", "bar"}; let hash: &HashSet<_> = &easy; assert_eq!(&easy, hash);
let easy: EasyMap<_, _> = map!{("foo", "bar"),}; let hash: &HashMap<_, _> = &easy; assert_eq!(&easy, hash); ```
Contributions welcome.