Sub

Trait Sub 

1.6.0 (const: unstable) · Source
pub trait Sub<Rhs = Self> {
    type Output;

    // Required method
    fn sub(self, rhs: Rhs) -> Self::Output;
}
Expand description

The subtraction operator -.

Note that Rhs is Self by default, but this is not mandatory. For example, std::time::SystemTime implements Sub<Duration>, which permits operations of the form SystemTime = SystemTime - Duration.

§Examples

§Subtractable points

use std::ops::Sub;

#[derive(Debug, Copy, Clone, PartialEq)]
struct Point {
    x: i32,
    y: i32,
}

impl Sub for Point {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
           Point { x: 1, y: 0 });

§Implementing Sub with generics

Here is an example of the same Point struct implementing the Sub trait using generics.

use std::ops::Sub;

#[derive(Debug, PartialEq)]
struct Point<T> {
    x: T,
    y: T,
}

// Notice that the implementation uses the associated type `Output`.
impl<T: Sub<Output = T>> Sub for Point<T> {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        Point {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

assert_eq!(Point { x: 2, y: 3 } - Point { x: 1, y: 0 },
           Point { x: 1, y: 3 });

Required Associated Types§

1.0.0 · Source

type Output

The resulting type after applying the - operator.

Required Methods§

1.0.0 · Source

fn sub(self, rhs: Rhs) -> Self::Output

Performs the - operation.

§Example
assert_eq!(12 - 1, 11);

Implementors§

1.0.0 (const: unstable) · Source§

impl Sub for f32

1.0.0 (const: unstable) · Source§

impl Sub for f64

1.0.0 (const: unstable) · Source§

impl Sub for i8

1.0.0 (const: unstable) · Source§

impl Sub for i16

1.0.0 (const: unstable) · Source§

impl Sub for i32

1.0.0 (const: unstable) · Source§

impl Sub for i64

1.0.0 (const: unstable) · Source§

impl Sub for i128

1.0.0 (const: unstable) · Source§

impl Sub for isize

1.0.0 (const: unstable) · Source§

impl Sub for u8

1.0.0 (const: unstable) · Source§

impl Sub for u16

1.0.0 (const: unstable) · Source§

impl Sub for u32

1.0.0 (const: unstable) · Source§

impl Sub for u64

1.0.0 (const: unstable) · Source§

impl Sub for u128

1.0.0 (const: unstable) · Source§

impl Sub for usize

1.0.0 (const: unstable) · Source§

impl Sub<&f32> for &f32

1.0.0 (const: unstable) · Source§

impl Sub<&f32> for f32

1.0.0 (const: unstable) · Source§

impl Sub<&f64> for &f64

1.0.0 (const: unstable) · Source§

impl Sub<&f64> for f64

1.0.0 (const: unstable) · Source§

impl Sub<&i8> for &i8

1.0.0 (const: unstable) · Source§

impl Sub<&i8> for i8

1.0.0 (const: unstable) · Source§

impl Sub<&i16> for &i16

1.0.0 (const: unstable) · Source§

impl Sub<&i16> for i16

1.0.0 (const: unstable) · Source§

impl Sub<&i32> for &i32

1.0.0 (const: unstable) · Source§

impl Sub<&i32> for i32

1.0.0 (const: unstable) · Source§

impl Sub<&i64> for &i64

1.0.0 (const: unstable) · Source§

impl Sub<&i64> for i64

1.0.0 (const: unstable) · Source§

impl Sub<&i128> for &i128

1.0.0 (const: unstable) · Source§

impl Sub<&i128> for i128

1.0.0 (const: unstable) · Source§

impl Sub<&isize> for &isize

1.0.0 (const: unstable) · Source§

impl Sub<&isize> for isize

1.0.0 (const: unstable) · Source§

impl Sub<&u8> for &u8

1.0.0 (const: unstable) · Source§

impl Sub<&u8> for u8

1.0.0 (const: unstable) · Source§

impl Sub<&u16> for &u16

1.0.0 (const: unstable) · Source§

impl Sub<&u16> for u16

1.0.0 (const: unstable) · Source§

impl Sub<&u32> for &u32

1.0.0 (const: unstable) · Source§

impl Sub<&u32> for u32

1.0.0 (const: unstable) · Source§

impl Sub<&u64> for &u64

1.0.0 (const: unstable) · Source§

impl Sub<&u64> for u64

1.0.0 (const: unstable) · Source§

impl Sub<&u128> for &u128

1.0.0 (const: unstable) · Source§

impl Sub<&u128> for u128

1.0.0 (const: unstable) · Source§

impl Sub<&usize> for &usize

1.0.0 (const: unstable) · Source§

impl Sub<&usize> for usize

1.0.0 (const: unstable) · Source§

impl Sub<f32> for &f32

1.0.0 (const: unstable) · Source§

impl Sub<f64> for &f64

1.0.0 (const: unstable) · Source§

impl Sub<i8> for &i8

1.0.0 (const: unstable) · Source§

impl Sub<i16> for &i16

1.0.0 (const: unstable) · Source§

impl Sub<i32> for &i32

1.0.0 (const: unstable) · Source§

impl Sub<i64> for &i64

1.0.0 (const: unstable) · Source§

impl Sub<i128> for &i128

1.0.0 (const: unstable) · Source§

impl Sub<isize> for &isize

1.0.0 (const: unstable) · Source§

impl Sub<u8> for &u8

1.0.0 (const: unstable) · Source§

impl Sub<u16> for &u16

1.0.0 (const: unstable) · Source§

impl Sub<u32> for &u32

1.0.0 (const: unstable) · Source§

impl Sub<u64> for &u64

1.0.0 (const: unstable) · Source§

impl Sub<u128> for &u128

1.0.0 (const: unstable) · Source§

impl Sub<usize> for &usize