Why does PartialEq not auto implement eq?

Implementing PartialEq requires implementing eq.
However is implementing ne not always more optimal?

Proving inequality is as simple as finding the first thing not equal to each other. While proving equality always demands proving everything equals.

For most structs that is a difference within constant times, but for containers it can be Linear. So most of the time it won't matter much, but still.

Can someone find an example where it would be faster to implement eq and auto impl ne, or barring that explain why ne was chosen as the one with a default implementation?

As soon as you find something that is not equal, ne can stop and return true and eq can stop and return false. So both require the same amount of checking.

3 Likes

Wow. I swear I spend a while contemplating this, but you're right. They both have to do equal work before they can short circuit.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.