How implement different key comparision of HashMap on a same struct

I have a struct as following:

struct Ingredient {
  name: String,
  alias: String,
  ...   //other data

I want to use this struct as key of two HashMap , one only compares field name, and another only compares field alias.

h_name: HashMap<Ingredient, usize> = HashMap::new();
h_alias: HashMap<Ingredient, usize> = HashMap::new();

How can I implement it? I dont want to split this struct to two structs because many fields are tightly coupled.

The best solution would be to just use HashMap<String, usize>. If you need to get the rest of the Ingredient, you could use HashMap<String, (Ingredient, usize)>. Otherwise, if it has to have this shape, you can provide a custom hash and equality test via the raw entry API.

I can provide a custom hash and equality test for this struct. But I can only provide one test on the struct, how can I provide different test for two HashMap on the same struct?

To clarify, you would look up entries in the HashMap::<String, usize>s with

let maybe_an_entry = h_name.get(&;
let maybe_an_entry = h_alias.get(&ingredient.alias);

Or is there some reason you need Ingredient to be the key?

(Oops, didn't mean for this to be a post-specific reply, sorry.)

The raw_entry allows you to provide the hash/eq test at key lookup time rather than as a type argument. It's nightly-only, but the hashbrown crate allows you to use it on stable. (The hashbrown crate is actually the implementor for the std hashmap.)

You could use a newtype for each one:

struct IngredientName(Ingredient);
impl Borrow<str> for IngredientName {
    fn borrow(&self) -> &str {

struct IngredientAlias(Ingredient);
impl Borrow<str> for IngredientAlias {
    fn borrow(&self) -> &str {

Then you can create a HashMap<IngredientName, usize> as well as a HashMap<IngredientAlias, usize>, and .get() accepts a str (so you can do .get("some ingredient name")).