std::ops::SubAssign

Trait std::ops::SubAssign

#[lang = "sub_assign"]
pub trait SubAssign<Rhs = Self> {
    fn sub_assign(&mut self, rhs: Rhs);
}

The subtraction assignment operator -=.

Examples

This example creates a Point struct that implements the SubAssign trait, and then demonstrates sub-assigning to a mutable Point.

use std::ops::SubAssign;

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

impl SubAssign for Point {
    fn sub_assign(&mut self, other: Point) {
        *self = 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
    }
}

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

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.SubAssign.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部