1
Fork 0

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
This commit is contained in:
Matthew Piziak 2016-08-21 16:01:27 -04:00
parent 3c5a0fa45b
commit eb6d44d697

View file

@ -245,25 +245,38 @@ add_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
/// ///
/// # Examples /// # Examples
/// ///
/// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up /// This example creates a `Point` struct that implements the `Sub` trait, and
/// calling `sub`, and therefore, `main` prints `Subtracting!`. /// then demonstrates subtracting two `Point`s.
/// ///
/// ``` /// ```
/// use std::ops::Sub; /// use std::ops::Sub;
/// ///
/// struct Foo; /// #[derive(Debug)]
/// struct Point {
/// x: i32,
/// y: i32,
/// }
/// ///
/// impl Sub for Foo { /// impl Sub for Point {
/// type Output = Foo; /// type Output = Point;
/// ///
/// fn sub(self, _rhs: Foo) -> Foo { /// fn sub(self, other: Point) -> Point {
/// println!("Subtracting!"); /// Point {
/// self /// x: self.x - other.x,
/// y: self.y - other.y,
/// }
/// }
/// }
///
/// impl PartialEq for Point {
/// fn eq(&self, other: &Self) -> bool {
/// self.x == other.x && self.y == other.y
/// } /// }
/// } /// }
/// ///
/// fn main() { /// fn main() {
/// Foo - Foo; /// assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
/// Point { x: 1, y: 0 });
/// } /// }
/// ``` /// ```
#[lang = "sub"] #[lang = "sub"]
@ -1053,25 +1066,36 @@ add_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
/// ///
/// # Examples /// # Examples
/// ///
/// A trivial implementation of `SubAssign`. When `Foo -= Foo` happens, it ends up /// This example creates a `Point` struct that implements the `SubAssign`
/// calling `sub_assign`, and therefore, `main` prints `Subtracting!`. /// trait, and then demonstrates sub-assigning to a mutable `Point`.
/// ///
/// ``` /// ```
/// use std::ops::SubAssign; /// use std::ops::SubAssign;
/// ///
/// struct Foo; /// #[derive(Debug)]
/// struct Point {
/// x: i32,
/// y: i32,
/// }
/// ///
/// impl SubAssign for Foo { /// impl SubAssign for Point {
/// fn sub_assign(&mut self, _rhs: Foo) { /// fn sub_assign(&mut self, other: Point) {
/// println!("Subtracting!"); /// *self = Point {
/// x: self.x - other.x,
/// y: self.y - other.y,
/// };
/// } /// }
/// } /// }
/// ///
/// # #[allow(unused_assignments)] /// impl PartialEq for Point {
/// fn main() { /// fn eq(&self, other: &Self) -> bool {
/// let mut foo = Foo; /// self.x == other.x && self.y == other.y
/// foo -= Foo;
/// } /// }
/// }
///
/// let mut point = Point { x: 3, y: 3 };
/// point -= Point { x: 2, y: 3 };
/// assert_eq!(point, Point {x: 1, y: 0});
/// ``` /// ```
#[lang = "sub_assign"] #[lang = "sub_assign"]
#[stable(feature = "op_assign_traits", since = "1.8.0")] #[stable(feature = "op_assign_traits", since = "1.8.0")]