std::ops::Sub

Trait std::ops::Sub

#[lang = "sub"]
pub trait Sub<RHS = Self> {
    type Output;
    fn sub(self, rhs: RHS) -> Self::Output;
}

The subtraction operator -.

Examples

This example creates a Point struct that implements the Sub trait, and then demonstrates subtracting two Points.

use std::ops::Sub;

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

impl Sub for Point {
    type Output = Point;

    fn sub(self, other: Point) -> Point {
        Point {
            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() {
    assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
               Point { x: 1, y: 0 });
}

Note that RHS = 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.

Associated Types

The resulting type after applying the - operator

Required Methods

The method for the - operator

Implementors

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ops/trait.Sub.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部