Trait nom::lib::std::ops::Sub1.0.0[][src]

pub trait Sub<Rhs = Self> {
    type Output;
    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 });

Associated Types

The resulting type after applying the - operator.

Required methods

Performs the - operation.

Example
assert_eq!(12 - 1, 11);

Implementations on Foreign Types

Implementors

impl<Tz: TimeZone> Sub<FixedOffset> for DateTime<Tz>

impl<Tz: TimeZone> Sub<Duration> for Date<Tz>

impl<Tz: TimeZone> Sub<Date<Tz>> for Date<Tz>

impl<Tz: TimeZone> Sub<Duration> for DateTime<Tz>

impl<Tz: TimeZone> Sub<DateTime<Tz>> for DateTime<Tz>

impl<T, S> Sub<&'_ HashSet<T, S, Global>> for &HashSet<T, S> where
    T: Eq + Hash + Clone,
    S: BuildHasher + Default

impl<T, S1, S2> Sub<&'_ IndexSet<T, S2>> for &IndexSet<T, S1> where
    T: Eq + Hash + Clone,
    S1: BuildHasher + Default,
    S2: BuildHasher

impl Sub<AtFlags> for AtFlags

impl Sub<OFlag> for OFlag

impl Sub<FdFlag> for FdFlag

impl Sub<MsFlags> for MsFlags

impl Sub<FdFlag> for FdFlag

impl Sub<MsFlags> for MsFlags

impl Sub<Persona> for Persona

impl Sub<Options> for Options

impl Sub<SaFlags> for SaFlags

impl Sub<SFlag> for SFlag

impl Sub<Mode> for Mode

impl Sub<FsFlags> for FsFlags

impl Sub<TimeVal> for TimeVal

impl Sub<Duration> for Tm

impl Sub<Tm> for Tm