@@ -245,25 +245,38 @@ add_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
245
245
///
246
246
/// # Examples
247
247
///
248
- /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
249
- /// calling `sub`, and therefore, `main` prints `Subtracting!` .
248
+ /// This example creates a `Point` struct that implements the `Sub` trait, and
249
+ /// then demonstrates subtracting two `Point`s .
250
250
///
251
251
/// ```
252
252
/// use std::ops::Sub;
253
253
///
254
- /// struct Foo;
254
+ /// #[derive(Debug)]
255
+ /// struct Point {
256
+ /// x: i32,
257
+ /// y: i32,
258
+ /// }
255
259
///
256
- /// impl Sub for Foo {
257
- /// type Output = Foo ;
260
+ /// impl Sub for Point {
261
+ /// type Output = Point ;
258
262
///
259
- /// fn sub(self, _rhs: Foo) -> Foo {
260
- /// println!("Subtracting!");
261
- /// self
263
+ /// fn sub(self, other: Point) -> Point {
264
+ /// Point {
265
+ /// x: self.x - other.x,
266
+ /// y: self.y - other.y,
267
+ /// }
268
+ /// }
269
+ /// }
270
+ ///
271
+ /// impl PartialEq for Point {
272
+ /// fn eq(&self, other: &Self) -> bool {
273
+ /// self.x == other.x && self.y == other.y
262
274
/// }
263
275
/// }
264
276
///
265
277
/// fn main() {
266
- /// Foo - Foo;
278
+ /// assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
279
+ /// Point { x: 1, y: 0 });
267
280
/// }
268
281
/// ```
269
282
#[ lang = "sub" ]
@@ -1156,25 +1169,36 @@ add_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
1156
1169
///
1157
1170
/// # Examples
1158
1171
///
1159
- /// A trivial implementation of `SubAssign`. When `Foo -= Foo` happens, it ends up
1160
- /// calling `sub_assign` , and therefore, `main` prints `Subtracting! `.
1172
+ /// This example creates a `Point` struct that implements the `SubAssign`
1173
+ /// trait , and then demonstrates sub-assigning to a mutable `Point `.
1161
1174
///
1162
1175
/// ```
1163
1176
/// use std::ops::SubAssign;
1164
1177
///
1165
- /// struct Foo;
1178
+ /// #[derive(Debug)]
1179
+ /// struct Point {
1180
+ /// x: i32,
1181
+ /// y: i32,
1182
+ /// }
1166
1183
///
1167
- /// impl SubAssign for Foo {
1168
- /// fn sub_assign(&mut self, _rhs: Foo) {
1169
- /// println!("Subtracting!");
1184
+ /// impl SubAssign for Point {
1185
+ /// fn sub_assign(&mut self, other: Point) {
1186
+ /// *self = Point {
1187
+ /// x: self.x - other.x,
1188
+ /// y: self.y - other.y,
1189
+ /// };
1170
1190
/// }
1171
1191
/// }
1172
1192
///
1173
- /// # #[allow(unused_assignments)]
1174
- /// fn main() {
1175
- /// let mut foo = Foo;
1176
- /// foo -= Foo;
1193
+ /// impl PartialEq for Point {
1194
+ /// fn eq(&self, other: &Self) -> bool {
1195
+ /// self.x == other.x && self.y == other.y
1196
+ /// }
1177
1197
/// }
1198
+ ///
1199
+ /// let mut point = Point { x: 3, y: 3 };
1200
+ /// point -= Point { x: 2, y: 3 };
1201
+ /// assert_eq!(point, Point {x: 1, y: 0});
1178
1202
/// ```
1179
1203
#[ lang = "sub_assign" ]
1180
1204
#[ stable( feature = "op_assign_traits" , since = "1.8.0" ) ]
0 commit comments