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
Add
able 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 });
Associated Types
Required methods
Implementations on Foreign Types
1.8.0 · sourceimpl Add<Duration> for SystemTime
impl Add<Duration> for SystemTime
sourcepub fn add(self, dur: Duration) -> SystemTime
pub fn add(self, dur: Duration) -> SystemTime
Panics
This function may panic if the resulting point in time cannot be represented by the
underlying data structure. See SystemTime::checked_add
for a version without panic.
type Output = SystemTime
sourceimpl<const N: usize> Add<Simd<isize, N>> for Simd<isize, N> where
isize: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<isize, N>> for Simd<isize, N> where
isize: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl Add<&'_ Saturating<u32>> for &'_ Saturating<u32>
impl Add<&'_ Saturating<u32>> for &'_ Saturating<u32>
type Output = <Saturating<u32> as Add<Saturating<u32>>>::Output
pub fn add(
self,
other: &Saturating<u32>
) -> <Saturating<u32> as Add<Saturating<u32>>>::Output
sourceimpl<'lhs, 'rhs, T, const LANES: usize> Add<&'rhs Simd<T, LANES>> for &'lhs Simd<T, LANES> where
T: SimdElement,
Simd<T, LANES>: Add<Simd<T, LANES>>,
LaneCount<LANES>: SupportedLaneCount,
<Simd<T, LANES> as Add<Simd<T, LANES>>>::Output == Simd<T, LANES>,
impl<'lhs, 'rhs, T, const LANES: usize> Add<&'rhs Simd<T, LANES>> for &'lhs Simd<T, LANES> where
T: SimdElement,
Simd<T, LANES>: Add<Simd<T, LANES>>,
LaneCount<LANES>: SupportedLaneCount,
<Simd<T, LANES> as Add<Simd<T, LANES>>>::Output == Simd<T, LANES>,
sourceimpl Add<&'_ Saturating<u128>> for &'_ Saturating<u128>
impl Add<&'_ Saturating<u128>> for &'_ Saturating<u128>
type Output = <Saturating<u128> as Add<Saturating<u128>>>::Output
pub fn add(
self,
other: &Saturating<u128>
) -> <Saturating<u128> as Add<Saturating<u128>>>::Output
sourceimpl<const N: usize> Add<Simd<f32, N>> for Simd<f32, N> where
f32: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<f32, N>> for Simd<f32, N> where
f32: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl<const N: usize> Add<Simd<u32, N>> for Simd<u32, N> where
u32: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<u32, N>> for Simd<u32, N> where
u32: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl Add<&'_ Saturating<u128>> for Saturating<u128>
impl Add<&'_ Saturating<u128>> for Saturating<u128>
type Output = <Saturating<u128> as Add<Saturating<u128>>>::Output
pub fn add(
self,
other: &Saturating<u128>
) -> <Saturating<u128> as Add<Saturating<u128>>>::Output
sourceimpl<'a> Add<Saturating<usize>> for &'a Saturating<usize>
impl<'a> Add<Saturating<usize>> for &'a Saturating<usize>
type Output = <Saturating<usize> as Add<Saturating<usize>>>::Output
pub fn add(
self,
other: Saturating<usize>
) -> <Saturating<usize> as Add<Saturating<usize>>>::Output
sourceimpl<'a> Add<Saturating<i64>> for &'a Saturating<i64>
impl<'a> Add<Saturating<i64>> for &'a Saturating<i64>
type Output = <Saturating<i64> as Add<Saturating<i64>>>::Output
pub fn add(
self,
other: Saturating<i64>
) -> <Saturating<i64> as Add<Saturating<i64>>>::Output
sourceimpl Add<&'_ Saturating<u8>> for &'_ Saturating<u8>
impl Add<&'_ Saturating<u8>> for &'_ Saturating<u8>
type Output = <Saturating<u8> as Add<Saturating<u8>>>::Output
pub fn add(
self,
other: &Saturating<u8>
) -> <Saturating<u8> as Add<Saturating<u8>>>::Output
sourceimpl<'a> Add<Saturating<i128>> for &'a Saturating<i128>
impl<'a> Add<Saturating<i128>> for &'a Saturating<i128>
type Output = <Saturating<i128> as Add<Saturating<i128>>>::Output
pub fn add(
self,
other: Saturating<i128>
) -> <Saturating<i128> as Add<Saturating<i128>>>::Output
sourceimpl<'a> Add<Saturating<i16>> for &'a Saturating<i16>
impl<'a> Add<Saturating<i16>> for &'a Saturating<i16>
type Output = <Saturating<i16> as Add<Saturating<i16>>>::Output
pub fn add(
self,
other: Saturating<i16>
) -> <Saturating<i16> as Add<Saturating<i16>>>::Output
sourceimpl Add<&'_ Saturating<i64>> for Saturating<i64>
impl Add<&'_ Saturating<i64>> for Saturating<i64>
type Output = <Saturating<i64> as Add<Saturating<i64>>>::Output
pub fn add(
self,
other: &Saturating<i64>
) -> <Saturating<i64> as Add<Saturating<i64>>>::Output
sourceimpl Add<Saturating<i32>> for Saturating<i32>
impl Add<Saturating<i32>> for Saturating<i32>
type Output = Saturating<i32>
pub fn add(self, other: Saturating<i32>) -> Saturating<i32>
sourceimpl Add<&'_ Saturating<i128>> for Saturating<i128>
impl Add<&'_ Saturating<i128>> for Saturating<i128>
type Output = <Saturating<i128> as Add<Saturating<i128>>>::Output
pub fn add(
self,
other: &Saturating<i128>
) -> <Saturating<i128> as Add<Saturating<i128>>>::Output
sourceimpl<'a> Add<Saturating<isize>> for &'a Saturating<isize>
impl<'a> Add<Saturating<isize>> for &'a Saturating<isize>
type Output = <Saturating<isize> as Add<Saturating<isize>>>::Output
pub fn add(
self,
other: Saturating<isize>
) -> <Saturating<isize> as Add<Saturating<isize>>>::Output
sourceimpl Add<&'_ Saturating<u64>> for Saturating<u64>
impl Add<&'_ Saturating<u64>> for Saturating<u64>
type Output = <Saturating<u64> as Add<Saturating<u64>>>::Output
pub fn add(
self,
other: &Saturating<u64>
) -> <Saturating<u64> as Add<Saturating<u64>>>::Output
sourceimpl Add<&'_ Saturating<usize>> for &'_ Saturating<usize>
impl Add<&'_ Saturating<usize>> for &'_ Saturating<usize>
type Output = <Saturating<usize> as Add<Saturating<usize>>>::Output
pub fn add(
self,
other: &Saturating<usize>
) -> <Saturating<usize> as Add<Saturating<usize>>>::Output
sourceimpl Add<Saturating<isize>> for Saturating<isize>
impl Add<Saturating<isize>> for Saturating<isize>
type Output = Saturating<isize>
pub fn add(self, other: Saturating<isize>) -> Saturating<isize>
sourceimpl Add<Saturating<u128>> for Saturating<u128>
impl Add<Saturating<u128>> for Saturating<u128>
type Output = Saturating<u128>
pub fn add(self, other: Saturating<u128>) -> Saturating<u128>
sourceimpl<'a> Add<Saturating<u16>> for &'a Saturating<u16>
impl<'a> Add<Saturating<u16>> for &'a Saturating<u16>
type Output = <Saturating<u16> as Add<Saturating<u16>>>::Output
pub fn add(
self,
other: Saturating<u16>
) -> <Saturating<u16> as Add<Saturating<u16>>>::Output
sourceimpl<'a> Add<Saturating<u8>> for &'a Saturating<u8>
impl<'a> Add<Saturating<u8>> for &'a Saturating<u8>
type Output = <Saturating<u8> as Add<Saturating<u8>>>::Output
pub fn add(
self,
other: Saturating<u8>
) -> <Saturating<u8> as Add<Saturating<u8>>>::Output
sourceimpl<const N: usize> Add<Simd<u16, N>> for Simd<u16, N> where
u16: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<u16, N>> for Simd<u16, N> where
u16: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl Add<&'_ Saturating<u32>> for Saturating<u32>
impl Add<&'_ Saturating<u32>> for Saturating<u32>
type Output = <Saturating<u32> as Add<Saturating<u32>>>::Output
pub fn add(
self,
other: &Saturating<u32>
) -> <Saturating<u32> as Add<Saturating<u32>>>::Output
sourceimpl Add<&'_ Saturating<i128>> for &'_ Saturating<i128>
impl Add<&'_ Saturating<i128>> for &'_ Saturating<i128>
type Output = <Saturating<i128> as Add<Saturating<i128>>>::Output
pub fn add(
self,
other: &Saturating<i128>
) -> <Saturating<i128> as Add<Saturating<i128>>>::Output
sourceimpl Add<Saturating<usize>> for Saturating<usize>
impl Add<Saturating<usize>> for Saturating<usize>
type Output = Saturating<usize>
pub fn add(self, other: Saturating<usize>) -> Saturating<usize>
sourceimpl Add<&'_ Saturating<i32>> for Saturating<i32>
impl Add<&'_ Saturating<i32>> for Saturating<i32>
type Output = <Saturating<i32> as Add<Saturating<i32>>>::Output
pub fn add(
self,
other: &Saturating<i32>
) -> <Saturating<i32> as Add<Saturating<i32>>>::Output
sourceimpl<T, const LANES: usize> Add<Simd<T, LANES>> for &'_ Simd<T, LANES> where
T: SimdElement,
Simd<T, LANES>: Add<Simd<T, LANES>>,
LaneCount<LANES>: SupportedLaneCount,
<Simd<T, LANES> as Add<Simd<T, LANES>>>::Output == Simd<T, LANES>,
impl<T, const LANES: usize> Add<Simd<T, LANES>> for &'_ Simd<T, LANES> where
T: SimdElement,
Simd<T, LANES>: Add<Simd<T, LANES>>,
LaneCount<LANES>: SupportedLaneCount,
<Simd<T, LANES> as Add<Simd<T, LANES>>>::Output == Simd<T, LANES>,
sourceimpl<'a> Add<Saturating<u128>> for &'a Saturating<u128>
impl<'a> Add<Saturating<u128>> for &'a Saturating<u128>
type Output = <Saturating<u128> as Add<Saturating<u128>>>::Output
pub fn add(
self,
other: Saturating<u128>
) -> <Saturating<u128> as Add<Saturating<u128>>>::Output
sourceimpl<'a> Add<Saturating<u64>> for &'a Saturating<u64>
impl<'a> Add<Saturating<u64>> for &'a Saturating<u64>
type Output = <Saturating<u64> as Add<Saturating<u64>>>::Output
pub fn add(
self,
other: Saturating<u64>
) -> <Saturating<u64> as Add<Saturating<u64>>>::Output
sourceimpl Add<Saturating<i64>> for Saturating<i64>
impl Add<Saturating<i64>> for Saturating<i64>
type Output = Saturating<i64>
pub fn add(self, other: Saturating<i64>) -> Saturating<i64>
sourceimpl<'a> Add<Saturating<i32>> for &'a Saturating<i32>
impl<'a> Add<Saturating<i32>> for &'a Saturating<i32>
type Output = <Saturating<i32> as Add<Saturating<i32>>>::Output
pub fn add(
self,
other: Saturating<i32>
) -> <Saturating<i32> as Add<Saturating<i32>>>::Output
sourceimpl<T, const LANES: usize> Add<&'_ Simd<T, LANES>> for Simd<T, LANES> where
T: SimdElement,
Simd<T, LANES>: Add<Simd<T, LANES>>,
LaneCount<LANES>: SupportedLaneCount,
<Simd<T, LANES> as Add<Simd<T, LANES>>>::Output == Simd<T, LANES>,
impl<T, const LANES: usize> Add<&'_ Simd<T, LANES>> for Simd<T, LANES> where
T: SimdElement,
Simd<T, LANES>: Add<Simd<T, LANES>>,
LaneCount<LANES>: SupportedLaneCount,
<Simd<T, LANES> as Add<Simd<T, LANES>>>::Output == Simd<T, LANES>,
sourceimpl Add<&'_ Saturating<u16>> for &'_ Saturating<u16>
impl Add<&'_ Saturating<u16>> for &'_ Saturating<u16>
type Output = <Saturating<u16> as Add<Saturating<u16>>>::Output
pub fn add(
self,
other: &Saturating<u16>
) -> <Saturating<u16> as Add<Saturating<u16>>>::Output
sourceimpl<const N: usize> Add<Simd<i16, N>> for Simd<i16, N> where
i16: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<i16, N>> for Simd<i16, N> where
i16: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl<const N: usize> Add<Simd<i64, N>> for Simd<i64, N> where
i64: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<i64, N>> for Simd<i64, N> where
i64: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl Add<&'_ Saturating<isize>> for Saturating<isize>
impl Add<&'_ Saturating<isize>> for Saturating<isize>
type Output = <Saturating<isize> as Add<Saturating<isize>>>::Output
pub fn add(
self,
other: &Saturating<isize>
) -> <Saturating<isize> as Add<Saturating<isize>>>::Output
sourceimpl<const N: usize> Add<Simd<u8, N>> for Simd<u8, N> where
u8: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<u8, N>> for Simd<u8, N> where
u8: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl<const N: usize> Add<Simd<f64, N>> for Simd<f64, N> where
f64: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<f64, N>> for Simd<f64, N> where
f64: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl Add<Saturating<i8>> for Saturating<i8>
impl Add<Saturating<i8>> for Saturating<i8>
type Output = Saturating<i8>
pub fn add(self, other: Saturating<i8>) -> Saturating<i8>
sourceimpl Add<Saturating<u8>> for Saturating<u8>
impl Add<Saturating<u8>> for Saturating<u8>
type Output = Saturating<u8>
pub fn add(self, other: Saturating<u8>) -> Saturating<u8>
sourceimpl Add<&'_ Saturating<i8>> for &'_ Saturating<i8>
impl Add<&'_ Saturating<i8>> for &'_ Saturating<i8>
type Output = <Saturating<i8> as Add<Saturating<i8>>>::Output
pub fn add(
self,
other: &Saturating<i8>
) -> <Saturating<i8> as Add<Saturating<i8>>>::Output
sourceimpl<const N: usize> Add<Simd<u64, N>> for Simd<u64, N> where
u64: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<u64, N>> for Simd<u64, N> where
u64: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl<const N: usize> Add<Simd<usize, N>> for Simd<usize, N> where
usize: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<usize, N>> for Simd<usize, N> where
usize: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl Add<Saturating<u32>> for Saturating<u32>
impl Add<Saturating<u32>> for Saturating<u32>
type Output = Saturating<u32>
pub fn add(self, other: Saturating<u32>) -> Saturating<u32>
sourceimpl Add<&'_ Saturating<isize>> for &'_ Saturating<isize>
impl Add<&'_ Saturating<isize>> for &'_ Saturating<isize>
type Output = <Saturating<isize> as Add<Saturating<isize>>>::Output
pub fn add(
self,
other: &Saturating<isize>
) -> <Saturating<isize> as Add<Saturating<isize>>>::Output
sourceimpl Add<&'_ Saturating<u8>> for Saturating<u8>
impl Add<&'_ Saturating<u8>> for Saturating<u8>
type Output = <Saturating<u8> as Add<Saturating<u8>>>::Output
pub fn add(
self,
other: &Saturating<u8>
) -> <Saturating<u8> as Add<Saturating<u8>>>::Output
sourceimpl<const N: usize> Add<Simd<i8, N>> for Simd<i8, N> where
i8: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<i8, N>> for Simd<i8, N> where
i8: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl Add<Saturating<u16>> for Saturating<u16>
impl Add<Saturating<u16>> for Saturating<u16>
type Output = Saturating<u16>
pub fn add(self, other: Saturating<u16>) -> Saturating<u16>
sourceimpl Add<Saturating<i16>> for Saturating<i16>
impl Add<Saturating<i16>> for Saturating<i16>
type Output = Saturating<i16>
pub fn add(self, other: Saturating<i16>) -> Saturating<i16>
sourceimpl Add<&'_ Saturating<i8>> for Saturating<i8>
impl Add<&'_ Saturating<i8>> for Saturating<i8>
type Output = <Saturating<i8> as Add<Saturating<i8>>>::Output
pub fn add(
self,
other: &Saturating<i8>
) -> <Saturating<i8> as Add<Saturating<i8>>>::Output
sourceimpl Add<Saturating<u64>> for Saturating<u64>
impl Add<Saturating<u64>> for Saturating<u64>
type Output = Saturating<u64>
pub fn add(self, other: Saturating<u64>) -> Saturating<u64>
sourceimpl Add<&'_ Saturating<i16>> for Saturating<i16>
impl Add<&'_ Saturating<i16>> for Saturating<i16>
type Output = <Saturating<i16> as Add<Saturating<i16>>>::Output
pub fn add(
self,
other: &Saturating<i16>
) -> <Saturating<i16> as Add<Saturating<i16>>>::Output
sourceimpl Add<Saturating<i128>> for Saturating<i128>
impl Add<Saturating<i128>> for Saturating<i128>
type Output = Saturating<i128>
pub fn add(self, other: Saturating<i128>) -> Saturating<i128>
sourceimpl Add<&'_ Saturating<u64>> for &'_ Saturating<u64>
impl Add<&'_ Saturating<u64>> for &'_ Saturating<u64>
type Output = <Saturating<u64> as Add<Saturating<u64>>>::Output
pub fn add(
self,
other: &Saturating<u64>
) -> <Saturating<u64> as Add<Saturating<u64>>>::Output
sourceimpl<const N: usize> Add<Simd<i32, N>> for Simd<i32, N> where
i32: SimdElement,
LaneCount<N>: SupportedLaneCount,
impl<const N: usize> Add<Simd<i32, N>> for Simd<i32, N> where
i32: SimdElement,
LaneCount<N>: SupportedLaneCount,
sourceimpl Add<&'_ Saturating<i32>> for &'_ Saturating<i32>
impl Add<&'_ Saturating<i32>> for &'_ Saturating<i32>
type Output = <Saturating<i32> as Add<Saturating<i32>>>::Output
pub fn add(
self,
other: &Saturating<i32>
) -> <Saturating<i32> as Add<Saturating<i32>>>::Output
sourceimpl<'a> Add<Saturating<u32>> for &'a Saturating<u32>
impl<'a> Add<Saturating<u32>> for &'a Saturating<u32>
type Output = <Saturating<u32> as Add<Saturating<u32>>>::Output
pub fn add(
self,
other: Saturating<u32>
) -> <Saturating<u32> as Add<Saturating<u32>>>::Output
sourceimpl Add<&'_ Saturating<u16>> for Saturating<u16>
impl Add<&'_ Saturating<u16>> for Saturating<u16>
type Output = <Saturating<u16> as Add<Saturating<u16>>>::Output
pub fn add(
self,
other: &Saturating<u16>
) -> <Saturating<u16> as Add<Saturating<u16>>>::Output
sourceimpl Add<&'_ Saturating<i16>> for &'_ Saturating<i16>
impl Add<&'_ Saturating<i16>> for &'_ Saturating<i16>
type Output = <Saturating<i16> as Add<Saturating<i16>>>::Output
pub fn add(
self,
other: &Saturating<i16>
) -> <Saturating<i16> as Add<Saturating<i16>>>::Output
sourceimpl<'a> Add<Saturating<i8>> for &'a Saturating<i8>
impl<'a> Add<Saturating<i8>> for &'a Saturating<i8>
type Output = <Saturating<i8> as Add<Saturating<i8>>>::Output
pub fn add(
self,
other: Saturating<i8>
) -> <Saturating<i8> as Add<Saturating<i8>>>::Output
sourceimpl Add<&'_ Saturating<i64>> for &'_ Saturating<i64>
impl Add<&'_ Saturating<i64>> for &'_ Saturating<i64>
type Output = <Saturating<i64> as Add<Saturating<i64>>>::Output
pub fn add(
self,
other: &Saturating<i64>
) -> <Saturating<i64> as Add<Saturating<i64>>>::Output
sourceimpl Add<&'_ Saturating<usize>> for Saturating<usize>
impl Add<&'_ Saturating<usize>> for Saturating<usize>
type Output = <Saturating<usize> as Add<Saturating<usize>>>::Output
pub fn add(
self,
other: &Saturating<usize>
) -> <Saturating<usize> as Add<Saturating<usize>>>::Output
Implementors
sourceimpl Add<&'_ str> for String
impl Add<&'_ str> for String
Implements the +
operator for concatenating two strings.
This consumes the String
on the left-hand side and re-uses its buffer (growing it if
necessary). This is done to avoid allocating a new String
and copying the entire contents on
every operation, which would lead to O(n^2) running time when building an n-byte string by
repeated concatenation.
The string on the right-hand side is only borrowed; its contents are copied into the returned
String
.
Examples
Concatenating two String
s takes the first by value and borrows the second:
let a = String::from("hello");
let b = String::from(" world");
let c = a + &b;
// `a` is moved and can no longer be used here.
If you want to keep using the first String
, you can clone it and append to the clone instead:
let a = String::from("hello");
let b = String::from(" world");
let c = a.clone() + &b;
// `a` is still valid here.
Concatenating &str
slices can be done by converting the first to a String
:
let a = "hello";
let b = " world";
let c = a.to_string() + b;