PartialEq on Rc of dyn Trait


let's say i want to pass a struct of parameters containing a member which is some trait's implementation.
The trait implementation is allocated somewhere else and is passed as reference (immutable, so
Rcis okay).
The parameter struct itself needs to be PartialEq, so i came up with this code:

I understand that Rust here refuses to apply PartialEq to Rc<dyn MyTrait>, but why?
Isn't it that any reference is an address in memory which can be compared?
What's the problem here, can someone explain it?


The == operator does not use the address to compute equality. Instead, it uses the equality operation on the underlying type. So, the problem is that dyn MyTrait doesn't implement PartialEq. This way, Rc::new(3) == Rc::new(3) is true even though the addresses will be different.

If you want to use pointer equality, then you can implement equality manually:

struct Params {
    mytrait: Rc<dyn MyTrait>,

impl PartialEq for Params {
    fn eq(&self, other: &Params) -> bool {
        Rc::ptr_eq(&self.mytrait, &other.mytrait)

impl Eq for Params {}

Thank you! :heart:

It's not relevant in this case, but when T: Eq, == on Rc<T> short-circuits using pointer equality and only compares the underlying values when the pointers differ. std docs