pub trait AddAssign<Rhs = Self> {
// Required method
fn add_assign(&mut self, rhs: Rhs);
}
Expand description
加法赋值运算符 +=
。
Examples
本示例创建一个 Point
结构体,该结构体实现 AddAssign
trait,然后演示对可变 Point
的添加分配。
use std::ops::AddAssign;
#[derive(Debug, Copy, Clone, PartialEq)]
struct Point {
x: i32,
y: i32,
}
impl AddAssign for Point {
fn add_assign(&mut self, other: Self) {
*self = Self {
x: self.x + other.x,
y: self.y + other.y,
};
}
}
let mut point = Point { x: 1, y: 0 };
point += Point { x: 2, y: 3 };
assert_eq!(point, Point { x: 3, y: 3 });
Run