Trait core::ops::AddStable [-]  [+] [src]

pub trait Add<RHS = Self> {
    type Output;

    fn add(self, rhs: RHS) -> Output;
}

The Add trait is used to specify the functionality of +.

Example

A trivial implementation of Add. When Foo + Foo happens, it ends up calling add, and therefore, main prints Adding!.

#![feature(associated_types)] use std::ops::Add; #[derive(Copy)] struct Foo; impl Add for Foo { type Output = Foo; fn add(self, _rhs: Foo) -> Foo { println!("Adding!"); self } } fn main() { Foo + Foo; }
#![feature(associated_types)]

use std::ops::Add;

#[derive(Copy)]
struct Foo;

impl Add for Foo {
    type Output = Foo;

    fn add(self, _rhs: Foo) -> Foo {
      println!("Adding!");
      self
  }
}

fn main() {
  Foo + Foo;
}

Associated Types

type Output

Required Methods

fn add(self, rhs: RHS) -> Output

The method for the + operator

Implementors