I have a number of Event
s like this:
struct Event {
a: String,
b: String,
v: u32,
}
and I would like to sum v
by a
and b
:
To do that I store them in a BTreeMap
. The only way to do that (as far as I know) is to define a struct for the keys:
#[derive(Ord,PartialOrd,Eq,PartialEq)]
struct Key {
a: String,
b: String,
}
let map = BTreeMap<Key, u32>;
Now, if I want to look up a group, I have to construct a Key
by cloning the strings for a
and b
:
let a = "abc";
let b = "abc";
let key = Key {
a: a.clone(),
b: b.clone(),
};
Even if I'd define a struct for borrowed strings:
struct RefKey {
a: &String,
b: &String,
}
that RefKey
can't be compared to a Key
.
In general owned strings can be compared to references. Can I tell the compiler to extend that property to my pair of structs? Or maybe I didn't choose my map keys well?