I'm extremely new to Rust and I'm trying to build a first crate! I am writing a function that takes a HashMap as its argument. I have it working, but Clippy is barking at me saying that I should be mindful of callers that may want to provide a different build hasher. I can't seem to find the best way to do this. I was able to get the warning to go away by just mandating that RandomState must be the third parameter:
This works and there's no warning. It's good that it's no longer implicit, but this didn't really solve any problem besides making the limitation explicit.
I imagine this would be a wonderful opportunity to learn from the Clippy docs, but they are severely lacking in providing breadcrumbs on how one would resolve this issue.
I tried doing some reading about BuildHasherDefault, although I admittedly had some trouble making the connection. I naively tried this:
Although this is valid, the calling code no longer works and I think I ran into a dead end on this line. I was hoping to have RandomState be the "default" if possible and allow it to be overridden, but those may touch on concepts I have not learned yet.
OK now I feel stupid. That seemed too easy and it seems to have worked so far. I have been working with scripting languages for far too long and maybe forgot how simple generics can be used.
So this is working because HashMap knows what a generic in position 3 must satisfy?
For functions in the second block, you can see the hasher used doesn't actually matter. I'm guessing you're only using the iterator functions on the HashMap? Those are in the second block. For the other accessors on the map, you need to do what @RustyYato suggested.
Ahhh. OK I will give it a more thorough read. Your first answer with the new context seems like it is totally sufficient then, as you are correct and I am only iterating.