bring back the example i removed, also add symmetry and simplify impl
This commit is contained in:
parent
423a5bb5c4
commit
1445a065de
1 changed files with 42 additions and 0 deletions
|
|
@ -132,6 +132,48 @@ use self::Ordering::*;
|
|||
/// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`,
|
||||
/// we allow `BookFormat`s to be compared with `Book`s.
|
||||
///
|
||||
/// You can also combine these implementations to let the `==` operator work with
|
||||
/// two different types:
|
||||
///
|
||||
/// ```
|
||||
/// #[derive(PartialEq)]
|
||||
/// enum BookFormat {
|
||||
/// Paperback,
|
||||
/// Hardback,
|
||||
/// Ebook,
|
||||
/// }
|
||||
///
|
||||
/// struct Book {
|
||||
/// isbn: i32,
|
||||
/// format: BookFormat,
|
||||
/// }
|
||||
///
|
||||
/// impl PartialEq<BookFormat> for Book {
|
||||
/// fn eq(&self, other: &BookFormat) -> bool {
|
||||
/// self.format == *other
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl PartialEq<Book> for BookFormat {
|
||||
/// fn eq(&self, other: &Book) -> bool {
|
||||
/// *self == other.format
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl PartialEq for Book {
|
||||
/// fn eq(&self, other: &Book) -> bool {
|
||||
/// self.isbn == other.isbn
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
|
||||
/// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
|
||||
///
|
||||
/// assert!(b1 == BookFormat::Paperback);
|
||||
/// assert!(b1 != BookFormat::Ebook);
|
||||
/// assert!(b1 == b2);
|
||||
/// ```
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue