How to implement Eq and Hash for my own structs to use them as a HashMap key?

前端 未结 2 1267
渐次进展
渐次进展 2021-01-07 19:25

I have two structs, A and B, and I want to use a HashMap. I have a piece of code like this:

use std::collec         


        
2条回答
  •  臣服心动
    2021-01-07 19:59

    You can have the compiler derive these instances for you by inserting the following before your struct declaration:

    #[derive(PartialEq, Eq, Hash)]
    pub struct A {
        // ...
    }
    

    You could also implement them manually instead. If you want to do that, you should read the documentation on traits, Eq and Hash.

提交回复
热议问题