Map diff library, or snippet equivalent to Clojure's clojure.data/diff?


#1

Hi, I came from Clojure land, and am newbie in Rust.

In Clojure, comparing Map A to B can be done with clojure.data/diff.
This function returns tuple of [things-only-in-a things-only-in-b things-in-both].
So for instance,

(clojure.data/diff {:a 1} {:a 1 :b 2})
will return:
=> (nil {:b 2} {:a 1})

I wonder if there’s something similar in Rust.
In fact, clojure.data/diff does much more than map to map diff, but for now, I’m basically interested in map to map comparison.

Thanks!


#2

I guess it only compares the keys, and not their values, right?

{BTree,Hash}Map don’t have built-in methods for this, but {BTree,Hash}Set have difference, symmetric_difference, intersection, union, etc. I couldn’t find a crate that does what you asked for (but I haven’t looked thoroughly). Maybe you can write one?


#3

It actually compares the values too.

(clojure.data/diff {:a 1 :c 1} {:a 1 :b 2 :c 3})
returns:
({:c 1} {:c 3, :b 2} {:a 1})

Thanks for checking. I was also doing some googling and couldn’t find them, so I was thinking that might be the case… I’ll probably iterate through the key|value pair and generate diff that I need.

Thanks!