|
21 | 21 | //! custom operators are required, you should look toward macros or compiler
|
22 | 22 | //! plugins to extend Rust's syntax.
|
23 | 23 | //!
|
| 24 | +//! Implementations of operator traits should be unsurprising in their |
| 25 | +//! respective contexts, keeping in mind their usual meanings and |
| 26 | +//! [operator precedence]. For example, when implementing [`Mul`], the operation |
| 27 | +//! should have some resemblance to multiplication (and share expected |
| 28 | +//! properties like associativity). |
| 29 | +//! |
24 | 30 | //! Note that the `&&` and `||` operators short-circuit, i.e. they only
|
25 | 31 | //! evaluate their second operand if it contributes to the result. Since this
|
26 | 32 | //! behavior is not enforceable by traits, `&&` and `||` are not supported as
|
|
46 | 52 | //! ```rust
|
47 | 53 | //! use std::ops::{Add, Sub};
|
48 | 54 | //!
|
49 |
| -//! #[derive(Debug)] |
| 55 | +//! #[derive(Debug, PartialEq)] |
50 | 56 | //! struct Point {
|
51 | 57 | //! x: i32,
|
52 | 58 | //! y: i32,
|
|
67 | 73 | //! Point {x: self.x - other.x, y: self.y - other.y}
|
68 | 74 | //! }
|
69 | 75 | //! }
|
70 |
| -//! fn main() { |
71 |
| -//! println!("{:?}", Point {x: 1, y: 0} + Point {x: 2, y: 3}); |
72 |
| -//! println!("{:?}", Point {x: 1, y: 0} - Point {x: 2, y: 3}); |
73 |
| -//! } |
| 76 | +//! |
| 77 | +//! assert_eq!(Point {x: 3, y: 3}, Point {x: 1, y: 0} + Point {x: 2, y: 3}); |
| 78 | +//! assert_eq!(Point {x: -1, y: -3}, Point {x: 1, y: 0} - Point {x: 2, y: 3}); |
74 | 79 | //! ```
|
75 | 80 | //!
|
76 | 81 | //! See the documentation for each trait for an example implementation.
|
|
143 | 148 | //! [`FnOnce`]: trait.FnOnce.html
|
144 | 149 | //! [`Add`]: trait.Add.html
|
145 | 150 | //! [`Sub`]: trait.Sub.html
|
| 151 | +//! [`Mul`]: trait.Mul.html |
146 | 152 | //! [`clone`]: ../clone/trait.Clone.html#tymethod.clone
|
| 153 | +//! [operator precedence]: ../../reference/expressions.html#operator-precedence |
147 | 154 |
|
148 | 155 | #![stable(feature = "rust1", since = "1.0.0")]
|
149 | 156 |
|
|
0 commit comments