std::ops::Add
Trait std::ops::Add
#[lang = "add"] pub trait Add<RHS = Self> { type Output; fn add(self, rhs: RHS) -> Self::Output; }
The addition operator +
.
Examples
This example creates a Point
struct that implements the Add
trait, and then demonstrates adding two Point
s.
use std::ops::Add; #[derive(Debug)] struct Point { x: i32, y: i32, } impl Add for Point { type Output = Point; fn add(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: 1, y: 0 } + Point { x: