I have a struct, and ultimately I'd like to be able to access the fields of this struct through a hash for the purposes of comparing individual fields equality with other instances of the same setup.
The documentation for the Eq
trait seems to say that Eq
is implemented for the primitive types (u8
, i16
, etc.). My goal is to store references to Eq
in a map and then do equality comparison through them.
struct User {
id: u32,
age: u8,
fingers: u8,
toes: u8,
}
let u1 = User { id: 1234, age: 42, fingers: 10, toes: 10 };
let u2 = User { id: 5678, age: 41, fingers: 10, toes: 10 };
let mut umap1: HashMap<String, &dyn Eq> = HashMap::new();
// ^^^^^^^ - Wrong... but I don't know if this is even possible.
What I'd like to do is insert into the hash something like:
umap1["id"] = &u1.id
umap1["age"] = &u1.age
I could then have a second hash (umap2
) and do something like this:
if umap1["id"] == umap2["id"]
Possible?
I believe How to test for equality between trait objects? is not really the same thing because it testing for equality between enums. I'd like to be able to take an Eq
reference to a primitive type (like a u16
, etc.) and compare it to another.