@@ -53,16 +53,43 @@ use option::Option::{self, Some};
53
53
/// symmetrically and transitively: if `T: PartialEq<U>` and `U: PartialEq<V>`
54
54
/// then `U: PartialEq<T>` and `T: PartialEq<V>`.
55
55
///
56
- /// PartialEq only requires the `eq` method to be implemented; `ne` is defined
57
- /// in terms of it by default. Any manual implementation of `ne` *must* respect
58
- /// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and
59
- /// only if `a != b`.
56
+ /// ## Derivable
60
57
///
61
58
/// This trait can be used with `#[derive]`. When `derive`d on structs, two
62
59
/// instances are equal if all fields are equal, and non equal if any fields
63
60
/// are not equal. When `derive`d on enums, each variant is equal to itself
64
61
/// and not equal to the other variants.
65
62
///
63
+ /// ## How can I implement `PartialEq`?
64
+ ///
65
+ /// PartialEq only requires the `eq` method to be implemented; `ne` is defined
66
+ /// in terms of it by default. Any manual implementation of `ne` *must* respect
67
+ /// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and
68
+ /// only if `a != b`.
69
+ ///
70
+ /// An example implementation for a domain in which two books are considered
71
+ /// the same book if their ISBN matches, even if the formats differ:
72
+ ///
73
+ /// ```
74
+ /// enum BookFormat { Paperback, Hardback, Ebook }
75
+ /// struct Book {
76
+ /// isbn: i32,
77
+ /// format: BookFormat,
78
+ /// }
79
+ /// impl PartialEq for Book {
80
+ /// fn eq(&self, other: &Self) -> bool {
81
+ /// self.isbn == other.isbn
82
+ /// }
83
+ /// }
84
+ ///
85
+ /// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
86
+ /// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
87
+ /// let b3 = Book { isbn: 10, format: BookFormat::Paperback };
88
+ ///
89
+ /// assert!(b1 == b2);
90
+ /// assert!(b1 != b3);
91
+ /// ```
92
+ ///
66
93
/// # Examples
67
94
///
68
95
/// ```
0 commit comments