pub trait Add<RHS = Self> { type Output; fn add(self, rhs: RHS) -> Self::Output; }
The Add
trait is used to specify the functionality of +
.
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: 2, y: 3 }, Point { x: 3, y: 3 }); }
Note that RHS = 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
.
type Output
The resulting type after applying the +
operator
impl<'a> Add<&'a str> for std::borrow::Cow<'a, str>
impl<'a> Add<Cow<'a, str>> for std::borrow::Cow<'a, str>
impl<'a> Add<&'a str> for String
impl Add<Wrapping<usize>> for std::num::Wrapping<usize>
impl Add<Wrapping<u8>> for std::num::Wrapping<u8>
impl Add<Wrapping<u16>> for std::num::Wrapping<u16>
impl Add<Wrapping<u32>> for std::num::Wrapping<u32>
impl Add<Wrapping<u64>> for std::num::Wrapping<u64>
impl Add<Wrapping<isize>> for std::num::Wrapping<isize>
impl Add<Wrapping<i8>> for std::num::Wrapping<i8>
impl Add<Wrapping<i16>> for std::num::Wrapping<i16>
impl Add<Wrapping<i32>> for std::num::Wrapping<i32>
impl Add<Wrapping<i64>> for std::num::Wrapping<i64>
impl<'a> Add<Wrapping<usize>> for &'a Wrapping<usize>
impl<'a> Add<&'a Wrapping<usize>> for std::num::Wrapping<usize>
impl<'a, 'b> Add<&'a Wrapping<usize>> for &'b Wrapping<usize>
impl<'a> Add<Wrapping<u8>> for &'a Wrapping<u8>
impl<'a> Add<&'a Wrapping<u8>> for std::num::Wrapping<u8>
impl<'a, 'b> Add<&'a Wrapping<u8>> for &'b Wrapping<u8>
impl<'a> Add<Wrapping<u16>> for &'a Wrapping<u16>
impl<'a> Add<&'a Wrapping<u16>> for std::num::Wrapping<u16>
impl<'a, 'b> Add<&'a Wrapping<u16>> for &'b Wrapping<u16>
impl<'a> Add<Wrapping<u32>> for &'a Wrapping<u32>
impl<'a> Add<&'a Wrapping<u32>> for std::num::Wrapping<u32>
impl<'a, 'b> Add<&'a Wrapping<u32>> for &'b Wrapping<u32>
impl<'a> Add<Wrapping<u64>> for &'a Wrapping<u64>
impl<'a> Add<&'a Wrapping<u64>> for std::num::Wrapping<u64>
impl<'a, 'b> Add<&'a Wrapping<u64>> for &'b Wrapping<u64>
impl<'a> Add<Wrapping<isize>> for &'a Wrapping<isize>
impl<'a> Add<&'a Wrapping<isize>> for std::num::Wrapping<isize>
impl<'a, 'b> Add<&'a Wrapping<isize>> for &'b Wrapping<isize>
impl<'a> Add<Wrapping<i8>> for &'a Wrapping<i8>
impl<'a> Add<&'a Wrapping<i8>> for std::num::Wrapping<i8>
impl<'a, 'b> Add<&'a Wrapping<i8>> for &'b Wrapping<i8>
impl<'a> Add<Wrapping<i16>> for &'a Wrapping<i16>
impl<'a> Add<&'a Wrapping<i16>> for std::num::Wrapping<i16>
impl<'a, 'b> Add<&'a Wrapping<i16>> for &'b Wrapping<i16>
impl<'a> Add<Wrapping<i32>> for &'a Wrapping<i32>
impl<'a> Add<&'a Wrapping<i32>> for std::num::Wrapping<i32>
impl<'a, 'b> Add<&'a Wrapping<i32>> for &'b Wrapping<i32>
impl<'a> Add<Wrapping<i64>> for &'a Wrapping<i64>
impl<'a> Add<&'a Wrapping<i64>> for std::num::Wrapping<i64>
impl<'a, 'b> Add<&'a Wrapping<i64>> for &'b Wrapping<i64>
impl Add<Wrapping<u128>> for std::num::Wrapping<u128>
impl Add<Wrapping<i128>> for std::num::Wrapping<i128>
impl<'a> Add<Wrapping<u128>> for &'a Wrapping<u128>
impl<'a> Add<&'a Wrapping<u128>> for std::num::Wrapping<u128>
impl<'a, 'b> Add<&'a Wrapping<u128>> for &'b Wrapping<u128>
impl<'a> Add<Wrapping<i128>> for &'a Wrapping<i128>
impl<'a> Add<&'a Wrapping<i128>> for std::num::Wrapping<i128>
impl<'a, 'b> Add<&'a Wrapping<i128>> for &'b Wrapping<i128>
impl Add<usize> for usize
impl Add<u8> for u8
impl Add<u16> for u16
impl Add<u32> for u32
impl Add<u64> for u64
impl Add<isize> for isize
impl Add<i8> for i8
impl Add<i16> for i16
impl Add<i32> for i32
impl Add<i64> for i64
impl Add<f32> for f32
impl Add<f64> for f64
impl<'a> Add<usize> for &'a usize
impl<'a> Add<&'a usize> for usize
impl<'a, 'b> Add<&'a usize> for &'b usize
impl<'a> Add<u8> for &'a u8
impl<'a> Add<&'a u8> for u8
impl<'a, 'b> Add<&'a u8> for &'b u8
impl<'a> Add<u16> for &'a u16
impl<'a> Add<&'a u16> for u16
impl<'a, 'b> Add<&'a u16> for &'b u16
impl<'a> Add<u32> for &'a u32
impl<'a> Add<&'a u32> for u32
impl<'a, 'b> Add<&'a u32> for &'b u32
impl<'a> Add<u64> for &'a u64
impl<'a> Add<&'a u64> for u64
impl<'a, 'b> Add<&'a u64> for &'b u64
impl<'a> Add<isize> for &'a isize
impl<'a> Add<&'a isize> for isize
impl<'a, 'b> Add<&'a isize> for &'b isize
impl<'a> Add<i8> for &'a i8
impl<'a> Add<&'a i8> for i8
impl<'a, 'b> Add<&'a i8> for &'b i8
impl<'a> Add<i16> for &'a i16
impl<'a> Add<&'a i16> for i16
impl<'a, 'b> Add<&'a i16> for &'b i16
impl<'a> Add<i32> for &'a i32
impl<'a> Add<&'a i32> for i32
impl<'a, 'b> Add<&'a i32> for &'b i32
impl<'a> Add<i64> for &'a i64
impl<'a> Add<&'a i64> for i64
impl<'a, 'b> Add<&'a i64> for &'b i64
impl<'a> Add<f32> for &'a f32
impl<'a> Add<&'a f32> for f32
impl<'a, 'b> Add<&'a f32> for &'b f32
impl<'a> Add<f64> for &'a f64
impl<'a> Add<&'a f64> for f64
impl<'a, 'b> Add<&'a f64> for &'b f64
impl Add<u128> for u128
impl Add<i128> for i128
impl<'a> Add<u128> for &'a u128
impl<'a> Add<&'a u128> for u128
impl<'a, 'b> Add<&'a u128> for &'b u128
impl<'a> Add<i128> for &'a i128
impl<'a> Add<&'a i128> for i128
impl<'a, 'b> Add<&'a i128> for &'b i128
impl Add for Duration
impl Add<Duration> for Instant
impl Add<Duration> for SystemTime
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ops/trait.Add.html