Trait std::ops::SubAssign1.8.0[][src]

pub trait SubAssign<Rhs = Self> {
    fn sub_assign(&mut self, rhs: Rhs);
}
Expand description

减法赋值运算符 -=

Examples

本示例创建一个实现 SubAssign trait 的 Point 结构体,然后演示对可变 Point 的子分配。

use std::ops::SubAssign;

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

impl SubAssign for Point {
    fn sub_assign(&mut self, other: Self) {
        *self = Self {
            x: self.x - other.x,
            y: self.y - other.y,
        };
    }
}

let mut point = Point { x: 3, y: 3 };
point -= Point { x: 2, y: 3 };
assert_eq!(point, Point {x: 1, y: 0});
Run

Required methods

执行 -= 操作。

Example

let mut x: u32 = 12;
x -= 1;
assert_eq!(x, 11);
Run

Implementors