Add

Trait Add 

1.6.0 (const: unstable) · Source
pub trait Add<Rhs = Self> {
    type Output;

    // Required method
    fn add(self, rhs: Rhs) -> Self::Output;
}
Expand description

The addition operator +.

Note that Rhs is Self by default, but this is not mandatory. For example, std::time::SystemTime implements Add<Duration>, which permits operations of the form SystemTime = SystemTime + Duration.

§Examples

§Addable points

use std::ops::Add;

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

impl Add for Point {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            x: self.x + other.x,
            y: self.y + other.y,
        }
    }
}

assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
           Point { x: 3, y: 3 });

§Implementing Add with generics

Here is an example of the same Point struct implementing the Add trait using generics.

use std::ops::Add;

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

// Notice that the implementation uses the associated type `Output`.
impl<T: Add<Output = T>> Add for Point<T> {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        Self {
            x: self.x + other.x,
            y: self.y + other.y,
        }
    }
}

assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
           Point { x: 3, y: 3 });

Required Associated Types§

1.0.0 · Source

type Output

The resulting type after applying the + operator.

Required Methods§

1.0.0 · Source

fn add(self, rhs: Rhs) -> Self::Output

Performs the + operation.

§Example
assert_eq!(12 + 1, 13);

Implementors§

1.0.0 (const: unstable) · Source§

impl Add for f32

1.0.0 (const: unstable) · Source§

impl Add for f64

1.0.0 (const: unstable) · Source§

impl Add for i8

1.0.0 (const: unstable) · Source§

impl Add for i16

1.0.0 (const: unstable) · Source§

impl Add for i32

1.0.0 (const: unstable) · Source§

impl Add for i64

1.0.0 (const: unstable) · Source§

impl Add for i128

1.0.0 (const: unstable) · Source§

impl Add for isize

1.0.0 (const: unstable) · Source§

impl Add for u8

1.0.0 (const: unstable) · Source§

impl Add for u16

1.0.0 (const: unstable) · Source§

impl Add for u32

1.0.0 (const: unstable) · Source§

impl Add for u64

1.0.0 (const: unstable) · Source§

impl Add for u128

1.0.0 (const: unstable) · Source§

impl Add for usize

1.0.0 (const: unstable) · Source§

impl Add<&f32> for &f32

1.0.0 (const: unstable) · Source§

impl Add<&f32> for f32

1.0.0 (const: unstable) · Source§

impl Add<&f64> for &f64

1.0.0 (const: unstable) · Source§

impl Add<&f64> for f64

1.0.0 (const: unstable) · Source§

impl Add<&i8> for &i8

1.0.0 (const: unstable) · Source§

impl Add<&i8> for i8

1.0.0 (const: unstable) · Source§

impl Add<&i16> for &i16

1.0.0 (const: unstable) · Source§

impl Add<&i16> for i16

1.0.0 (const: unstable) · Source§

impl Add<&i32> for &i32

1.0.0 (const: unstable) · Source§

impl Add<&i32> for i32

1.0.0 (const: unstable) · Source§

impl Add<&i64> for &i64

1.0.0 (const: unstable) · Source§

impl Add<&i64> for i64

1.0.0 (const: unstable) · Source§

impl Add<&i128> for &i128

1.0.0 (const: unstable) · Source§

impl Add<&i128> for i128

1.0.0 (const: unstable) · Source§

impl Add<&isize> for &isize

1.0.0 (const: unstable) · Source§

impl Add<&isize> for isize

1.0.0 (const: unstable) · Source§

impl Add<&u8> for &u8

1.0.0 (const: unstable) · Source§

impl Add<&u8> for u8

1.0.0 (const: unstable) · Source§

impl Add<&u16> for &u16

1.0.0 (const: unstable) · Source§

impl Add<&u16> for u16

1.0.0 (const: unstable) · Source§

impl Add<&u32> for &u32

1.0.0 (const: unstable) · Source§

impl Add<&u32> for u32

1.0.0 (const: unstable) · Source§

impl Add<&u64> for &u64

1.0.0 (const: unstable) · Source§

impl Add<&u64> for u64

1.0.0 (const: unstable) · Source§

impl Add<&u128> for &u128

1.0.0 (const: unstable) · Source§

impl Add<&u128> for u128

1.0.0 (const: unstable) · Source§

impl Add<&usize> for &usize

1.0.0 (const: unstable) · Source§

impl Add<&usize> for usize

1.0.0 (const: unstable) · Source§

impl Add<f32> for &f32

1.0.0 (const: unstable) · Source§

impl Add<f64> for &f64

1.0.0 (const: unstable) · Source§

impl Add<i8> for &i8

1.0.0 (const: unstable) · Source§

impl Add<i16> for &i16

1.0.0 (const: unstable) · Source§

impl Add<i32> for &i32

1.0.0 (const: unstable) · Source§

impl Add<i64> for &i64

1.0.0 (const: unstable) · Source§

impl Add<i128> for &i128

1.0.0 (const: unstable) · Source§

impl Add<isize> for &isize

1.0.0 (const: unstable) · Source§

impl Add<u8> for &u8

1.0.0 (const: unstable) · Source§

impl Add<u16> for &u16

1.0.0 (const: unstable) · Source§

impl Add<u32> for &u32

1.0.0 (const: unstable) · Source§

impl Add<u64> for &u64

1.0.0 (const: unstable) · Source§

impl Add<u128> for &u128

1.0.0 (const: unstable) · Source§

impl Add<usize> for &usize