Why does Rust have Eq and PartialEq?
Looking at the cmp docs, it says:
Eq and PartialEq are traits that allow you to define total and partial equality between values, respectively. Implementing them overloads the == and != operators.
Ord and PartialOrd are traits that allow you to define total and partial orderings between values, respectively. Implementing them overloads the <, <=, >, and >= operators.
Just from that, it appears the code would be different for Eq and PartialEq, but looking at the code, Eq extends PartialEq, but doesn't do anything. I am confused.
Looking at the Eq docs, you might find examples like this:
enum BookFormat { Paperback, Hardback, Ebook }
struct Book {
isbn: i32,
format: BookFormat,
}
impl PartialEq for Book {
fn eq(&self, other: &Self) -> bool {
self.isbn == other.isbn
}
}
impl Eq for Book {}
Or straight from the rust codebase:
impl PartialEq<IpAddr> for Ipv4Addr {
#[inline]
fn eq(&self, other: &IpAddr) -> bool {
match other {
IpAddr::V4(v4) => self == v4,
IpAddr::V6(_) => false,
}
}
}
impl Eq for Ipv4Addr {}
First question is, what does "Partial" mean in this rust context (Eq and Ord)? Why not just only have Eq and Ord and not PartialEq and PartialOrd? Then, why do you not do anything when implementing Eq? It appears to me that Eq and PartialEq (and same for Ord) are the same.
I am working on a programming language and using Rust as an inspiration, and I don't see why they implemented these traits like this.