@@ -245,25 +245,38 @@ add_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
245245///
246246/// # Examples
247247///
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 .
250250///
251251/// ```
252252/// use std::ops::Sub;
253253///
254- /// struct Foo;
254+ /// #[derive(Debug)]
255+ /// struct Point {
256+ /// x: i32,
257+ /// y: i32,
258+ /// }
255259///
256- /// impl Sub for Foo {
257- /// type Output = Foo ;
260+ /// impl Sub for Point {
261+ /// type Output = Point ;
258262///
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
262274/// }
263275/// }
264276///
265277/// fn main() {
266- /// Foo - Foo;
278+ /// assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
279+ /// Point { x: 1, y: 0 });
267280/// }
268281/// ```
269282#[ lang = "sub" ]
@@ -1156,25 +1169,36 @@ add_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
11561169///
11571170/// # Examples
11581171///
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 `.
11611174///
11621175/// ```
11631176/// use std::ops::SubAssign;
11641177///
1165- /// struct Foo;
1178+ /// #[derive(Debug)]
1179+ /// struct Point {
1180+ /// x: i32,
1181+ /// y: i32,
1182+ /// }
11661183///
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+ /// };
11701190/// }
11711191/// }
11721192///
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+ /// }
11771197/// }
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});
11781202/// ```
11791203#[ lang = "sub_assign" ]
11801204#[ stable( feature = "op_assign_traits" , since = "1.8.0" ) ]
0 commit comments