trait

Eq — full equivalence

Eq marks equality on the same type as reflexive, symmetric and transitive. It has no additional ordinary required method. Derive both PartialEq and Eq when every field supports them and the semantics fit. The compiler cannot prove that a manual comparison obeys the laws. Expected stdout (not executed): false Common mistake: Trying to derive Eq for a struct with a plain f64 field.

Formal Syntax & Structure

rust syntaxrust
trait Eq: PartialEq<Self>

Working Example

Example Snippetrust
#[derive(PartialEq, Eq)]struct Ticket(u32);fn equal<T: Eq>(a: &T, b: &T) -> bool { a == b }fn main() {    let a = Ticket(1);    let b = Ticket(2);    println!("{}", equal(&a, &b));}
Rust Reference