@@ -91,6 +91,8 @@ use self::Ordering::*;
91
91
/// For example, let's tweak our previous code a bit:
92
92
///
93
93
/// ```
94
+ /// // The derive implements <BookFormat> == <BookFormat> comparisons
95
+ /// #[derive(PartialEq)]
94
96
/// enum BookFormat {
95
97
/// Paperback,
96
98
/// Hardback,
@@ -102,31 +104,34 @@ use self::Ordering::*;
102
104
/// format: BookFormat,
103
105
/// }
104
106
///
107
+ /// // Implement <Book> == <BookFormat> comparisons
105
108
/// impl PartialEq<BookFormat> for Book {
106
109
/// fn eq(&self, other: &BookFormat) -> bool {
107
- /// match (&self.format, other) {
108
- /// (BookFormat::Paperback, BookFormat::Paperback) => true,
109
- /// (BookFormat::Hardback, BookFormat::Hardback) => true,
110
- /// (BookFormat::Ebook, BookFormat::Ebook) => true,
111
- /// (_, _) => false,
112
- /// }
110
+ /// self.format == *other
111
+ /// }
112
+ /// }
113
+ ///
114
+ /// // Implement <BookFormat> == <Book> comparisons
115
+ /// impl PartialEq<Book> for BookFormat {
116
+ /// fn eq(&self, other: &Book) -> bool {
117
+ /// *self == other.format
113
118
/// }
114
119
/// }
115
120
///
116
121
/// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
117
122
///
118
123
/// assert!(b1 == BookFormat::Paperback);
119
- /// assert!(b1 != BookFormat::Ebook);
124
+ /// assert!(BookFormat::Ebook != b1 );
120
125
/// ```
121
126
///
122
127
/// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`,
123
- /// we've changed what type we can use on the right side of the `==` operator.
124
- /// This lets us use it in the `assert!` statements at the bottom.
128
+ /// we allow `BookFormat`s to be compared with `Book`s.
125
129
///
126
130
/// You can also combine these implementations to let the `==` operator work with
127
131
/// two different types:
128
132
///
129
133
/// ```
134
+ /// #[derive(PartialEq)]
130
135
/// enum BookFormat {
131
136
/// Paperback,
132
137
/// Hardback,
@@ -140,12 +145,13 @@ use self::Ordering::*;
140
145
///
141
146
/// impl PartialEq<BookFormat> for Book {
142
147
/// fn eq(&self, other: &BookFormat) -> bool {
143
- /// match (&self.format, other) {
144
- /// (&BookFormat::Paperback, &BookFormat::Paperback) => true,
145
- /// (&BookFormat::Hardback, &BookFormat::Hardback) => true,
146
- /// (&BookFormat::Ebook, &BookFormat::Ebook) => true,
147
- /// (_, _) => false,
148
- /// }
148
+ /// self.format == *other
149
+ /// }
150
+ /// }
151
+ ///
152
+ /// impl PartialEq<Book> for BookFormat {
153
+ /// fn eq(&self, other: &Book) -> bool {
154
+ /// *self == other.format
149
155
/// }
150
156
/// }
151
157
///
@@ -159,7 +165,7 @@ use self::Ordering::*;
159
165
/// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
160
166
///
161
167
/// assert!(b1 == BookFormat::Paperback);
162
- /// assert!(b1 != BookFormat::Ebook);
168
+ /// assert!(BookFormat::Ebook != b1 );
163
169
/// assert!(b1 == b2);
164
170
/// ```
165
171
///
0 commit comments