Skip to content

Commit eb6d44d

Browse files
more evocative examples for Sub and SubAssign
These examples are exactly analogous to those in PRs #35709 and #35806. I'll probably remove the `fn main` wrappers for `Add` and `Sub` once this is merged in. Part of #29365. r? @steveklabnik
1 parent 3c5a0fa commit eb6d44d

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

src/libcore/ops.rs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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"]
@@ -1053,25 +1066,36 @@ add_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
10531066
///
10541067
/// # Examples
10551068
///
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`.
10581071
///
10591072
/// ```
10601073
/// use std::ops::SubAssign;
10611074
///
1062-
/// struct Foo;
1075+
/// #[derive(Debug)]
1076+
/// struct Point {
1077+
/// x: i32,
1078+
/// y: i32,
1079+
/// }
10631080
///
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+
/// };
10671087
/// }
10681088
/// }
10691089
///
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+
/// }
10741094
/// }
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});
10751099
/// ```
10761100
#[lang = "sub_assign"]
10771101
#[stable(feature = "op_assign_traits", since = "1.8.0")]

0 commit comments

Comments
 (0)