|
67 | 67 | //! }
|
68 | 68 | //! ```
|
69 | 69 | //!
|
70 |
| -//! See the documentation for each trait for a minimum implementation that |
71 |
| -//! prints something to the screen. |
| 70 | +//! See the documentation for each trait for an example implementation. |
72 | 71 |
|
73 | 72 | #![stable(feature = "rust1", since = "1.0.0")]
|
74 | 73 |
|
@@ -171,25 +170,38 @@ macro_rules! forward_ref_binop {
|
171 | 170 | ///
|
172 | 171 | /// # Examples
|
173 | 172 | ///
|
174 |
| -/// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up |
175 |
| -/// calling `add`, and therefore, `main` prints `Adding!`. |
| 173 | +/// This example creates a `Point` struct that implements the `Add` trait, and |
| 174 | +/// then demonstrates adding two `Point`s. |
176 | 175 | ///
|
177 | 176 | /// ```
|
178 | 177 | /// use std::ops::Add;
|
179 | 178 | ///
|
180 |
| -/// struct Foo; |
| 179 | +/// #[derive(Debug)] |
| 180 | +/// struct Point { |
| 181 | +/// x: i32, |
| 182 | +/// y: i32, |
| 183 | +/// } |
181 | 184 | ///
|
182 |
| -/// impl Add for Foo { |
183 |
| -/// type Output = Foo; |
| 185 | +/// impl Add for Point { |
| 186 | +/// type Output = Point; |
184 | 187 | ///
|
185 |
| -/// fn add(self, _rhs: Foo) -> Foo { |
186 |
| -/// println!("Adding!"); |
187 |
| -/// self |
| 188 | +/// fn add(self, other: Point) -> Point { |
| 189 | +/// Point { |
| 190 | +/// x: self.x + other.x, |
| 191 | +/// y: self.y + other.y, |
| 192 | +/// } |
| 193 | +/// } |
| 194 | +/// } |
| 195 | +/// |
| 196 | +/// impl PartialEq for Point { |
| 197 | +/// fn eq(&self, other: &Self) -> bool { |
| 198 | +/// self.x == other.x && self.y == other.y |
188 | 199 | /// }
|
189 | 200 | /// }
|
190 | 201 | ///
|
191 | 202 | /// fn main() {
|
192 |
| -/// Foo + Foo; |
| 203 | +/// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, |
| 204 | +/// Point { x: 3, y: 3 }); |
193 | 205 | /// }
|
194 | 206 | /// ```
|
195 | 207 | #[lang = "add"]
|
|
0 commit comments