I have a User
struct:
struct User {
id: i32,
email: String,
// ...
}
In one part of my code I want to get unique users by database ID but in another piece of code I want to get unique users by email address. I have worked on systems before where users got mapped to external system accounts using LDAP CNs, email, etc. and being able to map a user by a different ID in some situations is very useful.
In .NET you can pass in an IEqualityComparer
interface to override equals/hash for a particular Dictionary
. In C++, the unordered_map
class has generic parameters for the hash and eq functions. In Java, I've learned to just use Map
s instead of Set
s when I want to get unique values keyed off something, but this can be awkward, especially for compound keys.
Truthfully, this is a pretty rare situation and there's always the workaround of using maps instead of sets or creating a wrapper struct with its own Hash
/Eq
impl
block. I am just curious if there's a simpler way to do this in Rust that I am just not aware of yet.