@@ -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" ]
@@ -1053,25 +1066,36 @@ add_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
1053
1066
///
1054
1067
/// # Examples
1055
1068
///
1056
- /// A trivial implementation of `SubAssign`. When `Foo -= Foo` happens, it ends up
1057
- /// calling `sub_assign` , and therefore, `main` prints `Subtracting! `.
1069
+ /// This example creates a `Point` struct that implements the `SubAssign`
1070
+ /// trait , and then demonstrates sub-assigning to a mutable `Point `.
1058
1071
///
1059
1072
/// ```
1060
1073
/// use std::ops::SubAssign;
1061
1074
///
1062
- /// struct Foo;
1075
+ /// #[derive(Debug)]
1076
+ /// struct Point {
1077
+ /// x: i32,
1078
+ /// y: i32,
1079
+ /// }
1063
1080
///
1064
- /// impl SubAssign for Foo {
1065
- /// fn sub_assign(&mut self, _rhs: Foo) {
1066
- /// println!("Subtracting!");
1081
+ /// impl SubAssign for Point {
1082
+ /// fn sub_assign(&mut self, other: Point) {
1083
+ /// *self = Point {
1084
+ /// x: self.x - other.x,
1085
+ /// y: self.y - other.y,
1086
+ /// };
1067
1087
/// }
1068
1088
/// }
1069
1089
///
1070
- /// # #[allow(unused_assignments)]
1071
- /// fn main() {
1072
- /// let mut foo = Foo;
1073
- /// foo -= Foo;
1090
+ /// impl PartialEq for Point {
1091
+ /// fn eq(&self, other: &Self) -> bool {
1092
+ /// self.x == other.x && self.y == other.y
1093
+ /// }
1074
1094
/// }
1095
+ ///
1096
+ /// let mut point = Point { x: 3, y: 3 };
1097
+ /// point -= Point { x: 2, y: 3 };
1098
+ /// assert_eq!(point, Point {x: 1, y: 0});
1075
1099
/// ```
1076
1100
#[ lang = "sub_assign" ]
1077
1101
#[ stable( feature = "op_assign_traits" , since = "1.8.0" ) ]
0 commit comments