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

pub trait AddAssign<Rhs = Self> {
    fn add_assign(&mut self, Rhs);
}

The AddAssign trait is used to specify the functionality of +=.

Examples

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

use std::ops::AddAssign;

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

impl AddAssign for Point {
    fn add_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: 1, y: 0 };
point += Point { x: 2, y: 3 };
assert_eq!(point, Point { x: 3, y: 3 });Run

Required Methods

The method for the += operator

Implementors