The popular Rust crate for bidirectional maps, bimap, is limited to one-to-one relations (bijections) only (and appears unmaintained). But sometimes you need to have one-to-many and many-to-many relations, or have them made of arbitrary containers instead of having to always use either BTreeMap or HashMap.
I couldn't find anything for that, so I wrote my own Rust crate, multi_bimap:
It's fully generic over its underlying containers (two antiparallel maps of sets), so you can have an arbitrary setup. You can e.g. have two hash maps of hash sets, or two B-tree maps of vecs. You are not limited to many-to-many relations, you can also have type-enforced one-to-one, one-to-many bimaps. You can have different types for the left-to-right and right-to-left constituent maps. You can use third-party collection types, e.g. indexmap::IndexMap as a constituent map and tinyvec::TinyVec as a constituent set. You can put keys inside Rc to avoid Clones for larger data.
Here's a simple code example:
let mut produce: HashMultiBimap<&str, &str> = HashMultiBimap::new(); produce.insert("tomato", "red"); produce.insert("tomato", "fruit"); produce.insert("carrot", "orange"); produce.insert("carrot", "vegetable"); produce.insert("apple", "red"); produce.insert("apple", "fruit"); produce.insert("spinach", "green"); produce.insert("spinach", "vegetable"); assert_eq!( produce.get_by_left("tomato"), Some(&HashSet::from(["red", "fruit"])), ); assert_eq!( produce.get_by_right("fruit"), Some(&HashSet::from(["tomato", "apple"])), ); assert_eq!( produce.get_by_right("vegetable"), Some(&HashSet::from(["carrot", "spinach"])), ); 1 post - 1 participant

