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
§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 });
Required Associated Types§
Required Methods§
Implementors§
Source§impl Add for ConstDecimals<0usize>
impl Add for ConstDecimals<0usize>
type Output = ConstDecimals<0>
Source§impl Add for ConstDecimals<1usize>
impl Add for ConstDecimals<1usize>
type Output = ConstDecimals<2>
Source§impl Add for ConstDecimals<2usize>
impl Add for ConstDecimals<2usize>
type Output = ConstDecimals<4>
Source§impl Add for ConstDecimals<3usize>
impl Add for ConstDecimals<3usize>
type Output = ConstDecimals<6>
Source§impl Add for ConstDecimals<4usize>
impl Add for ConstDecimals<4usize>
type Output = ConstDecimals<8>
Source§impl Add for ConstDecimals<5usize>
impl Add for ConstDecimals<5usize>
type Output = ConstDecimals<10>
Source§impl Add for ConstDecimals<6usize>
impl Add for ConstDecimals<6usize>
type Output = ConstDecimals<12>
Source§impl Add for ConstDecimals<7usize>
impl Add for ConstDecimals<7usize>
type Output = ConstDecimals<14>
Source§impl Add for ConstDecimals<8usize>
impl Add for ConstDecimals<8usize>
type Output = ConstDecimals<16>
Source§impl Add for ConstDecimals<9usize>
impl Add for ConstDecimals<9usize>
type Output = ConstDecimals<18>
Source§impl Add for ConstDecimals<10usize>
impl Add for ConstDecimals<10usize>
type Output = ConstDecimals<20>
Source§impl Add for ConstDecimals<11usize>
impl Add for ConstDecimals<11usize>
type Output = ConstDecimals<22>
Source§impl Add for ConstDecimals<12usize>
impl Add for ConstDecimals<12usize>
type Output = ConstDecimals<24>
Source§impl Add for ConstDecimals<13usize>
impl Add for ConstDecimals<13usize>
type Output = ConstDecimals<26>
Source§impl Add for ConstDecimals<14usize>
impl Add for ConstDecimals<14usize>
type Output = ConstDecimals<28>
Source§impl Add for ConstDecimals<15usize>
impl Add for ConstDecimals<15usize>
type Output = ConstDecimals<30>
Source§impl Add for ConstDecimals<16usize>
impl Add for ConstDecimals<16usize>
type Output = ConstDecimals<32>
Source§impl Add for ConstDecimals<17usize>
impl Add for ConstDecimals<17usize>
type Output = ConstDecimals<34>
Source§impl Add for ConstDecimals<18usize>
impl Add for ConstDecimals<18usize>
type Output = ConstDecimals<36>
Source§impl Add for ConstDecimals<19usize>
impl Add for ConstDecimals<19usize>
type Output = ConstDecimals<38>
Source§impl Add for ConstDecimals<20usize>
impl Add for ConstDecimals<20usize>
type Output = ConstDecimals<40>
Source§impl Add for ConstDecimals<21usize>
impl Add for ConstDecimals<21usize>
type Output = ConstDecimals<42>
Source§impl Add for ConstDecimals<22usize>
impl Add for ConstDecimals<22usize>
type Output = ConstDecimals<44>
Source§impl Add for ConstDecimals<23usize>
impl Add for ConstDecimals<23usize>
type Output = ConstDecimals<46>
Source§impl Add for ConstDecimals<24usize>
impl Add for ConstDecimals<24usize>
type Output = ConstDecimals<48>
Source§impl Add for ConstDecimals<25usize>
impl Add for ConstDecimals<25usize>
type Output = ConstDecimals<50>
Source§impl Add for ConstDecimals<26usize>
impl Add for ConstDecimals<26usize>
type Output = ConstDecimals<52>
Source§impl Add for ConstDecimals<27usize>
impl Add for ConstDecimals<27usize>
type Output = ConstDecimals<54>
Source§impl Add for ConstDecimals<28usize>
impl Add for ConstDecimals<28usize>
type Output = ConstDecimals<56>
Source§impl Add for ConstDecimals<29usize>
impl Add for ConstDecimals<29usize>
type Output = ConstDecimals<58>
Source§impl Add for ConstDecimals<30usize>
impl Add for ConstDecimals<30usize>
type Output = ConstDecimals<60>
Source§impl Add for ConstDecimals<31usize>
impl Add for ConstDecimals<31usize>
type Output = ConstDecimals<62>
Source§impl Add for ConstDecimals<32usize>
impl Add for ConstDecimals<32usize>
type Output = ConstDecimals<64>
Source§impl Add for ConstDecimals<33usize>
impl Add for ConstDecimals<33usize>
type Output = ConstDecimals<66>
Source§impl Add for ConstDecimals<34usize>
impl Add for ConstDecimals<34usize>
type Output = ConstDecimals<68>
Source§impl Add for ConstDecimals<35usize>
impl Add for ConstDecimals<35usize>
type Output = ConstDecimals<70>
Source§impl Add for ConstDecimals<36usize>
impl Add for ConstDecimals<36usize>
type Output = ConstDecimals<72>
Source§impl Add for ConstDecimals<37usize>
impl Add for ConstDecimals<37usize>
type Output = ConstDecimals<74>
Source§impl Add for ConstDecimals<38usize>
impl Add for ConstDecimals<38usize>
type Output = ConstDecimals<76>
Source§impl Add for ConstDecimals<39usize>
impl Add for ConstDecimals<39usize>
type Output = ConstDecimals<78>
Source§impl Add for ConstDecimals<40usize>
impl Add for ConstDecimals<40usize>
type Output = ConstDecimals<80>
Source§impl Add for ConstDecimals<41usize>
impl Add for ConstDecimals<41usize>
type Output = ConstDecimals<82>
Source§impl Add for ConstDecimals<42usize>
impl Add for ConstDecimals<42usize>
type Output = ConstDecimals<84>
Source§impl Add for ConstDecimals<43usize>
impl Add for ConstDecimals<43usize>
type Output = ConstDecimals<86>
Source§impl Add for ConstDecimals<44usize>
impl Add for ConstDecimals<44usize>
type Output = ConstDecimals<88>
Source§impl Add for ConstDecimals<45usize>
impl Add for ConstDecimals<45usize>
type Output = ConstDecimals<90>
Source§impl Add for ConstDecimals<46usize>
impl Add for ConstDecimals<46usize>
type Output = ConstDecimals<92>
Source§impl Add for ConstDecimals<47usize>
impl Add for ConstDecimals<47usize>
type Output = ConstDecimals<94>
Source§impl Add for ConstDecimals<48usize>
impl Add for ConstDecimals<48usize>
type Output = ConstDecimals<96>
Source§impl Add for ConstDecimals<49usize>
impl Add for ConstDecimals<49usize>
type Output = ConstDecimals<98>
Source§impl Add for ConstDecimals<50usize>
impl Add for ConstDecimals<50usize>
type Output = ConstDecimals<100>
Source§impl Add for ConstDecimals<51usize>
impl Add for ConstDecimals<51usize>
type Output = ConstDecimals<102>
Source§impl Add for ConstDecimals<52usize>
impl Add for ConstDecimals<52usize>
type Output = ConstDecimals<104>
Source§impl Add for ConstDecimals<53usize>
impl Add for ConstDecimals<53usize>
type Output = ConstDecimals<106>
Source§impl Add for ConstDecimals<54usize>
impl Add for ConstDecimals<54usize>
type Output = ConstDecimals<108>
Source§impl Add for ConstDecimals<55usize>
impl Add for ConstDecimals<55usize>
type Output = ConstDecimals<110>
Source§impl Add for ConstDecimals<56usize>
impl Add for ConstDecimals<56usize>
type Output = ConstDecimals<112>
Source§impl Add for ConstDecimals<57usize>
impl Add for ConstDecimals<57usize>
type Output = ConstDecimals<114>
Source§impl Add for ConstDecimals<58usize>
impl Add for ConstDecimals<58usize>
type Output = ConstDecimals<116>
Source§impl Add for ConstDecimals<59usize>
impl Add for ConstDecimals<59usize>
type Output = ConstDecimals<118>
Source§impl Add for ConstDecimals<60usize>
impl Add for ConstDecimals<60usize>
type Output = ConstDecimals<120>
Source§impl Add for ConstDecimals<61usize>
impl Add for ConstDecimals<61usize>
type Output = ConstDecimals<122>
Source§impl Add for ConstDecimals<62usize>
impl Add for ConstDecimals<62usize>
type Output = ConstDecimals<124>
Source§impl Add for ConstDecimals<63usize>
impl Add for ConstDecimals<63usize>
type Output = ConstDecimals<126>
Source§impl Add for ConstDecimals<64usize>
impl Add for ConstDecimals<64usize>
type Output = ConstDecimals<128>
1.74.0 · Source§impl Add for Saturating<i8>
impl Add for Saturating<i8>
type Output = Saturating<i8>
1.74.0 · Source§impl Add for Saturating<i16>
impl Add for Saturating<i16>
type Output = Saturating<i16>
1.74.0 · Source§impl Add for Saturating<i32>
impl Add for Saturating<i32>
type Output = Saturating<i32>
1.74.0 · Source§impl Add for Saturating<i64>
impl Add for Saturating<i64>
type Output = Saturating<i64>
1.74.0 · Source§impl Add for Saturating<i128>
impl Add for Saturating<i128>
type Output = Saturating<i128>
1.74.0 · Source§impl Add for Saturating<isize>
impl Add for Saturating<isize>
type Output = Saturating<isize>
1.74.0 · Source§impl Add for Saturating<u8>
impl Add for Saturating<u8>
type Output = Saturating<u8>
1.74.0 · Source§impl Add for Saturating<u16>
impl Add for Saturating<u16>
type Output = Saturating<u16>
1.74.0 · Source§impl Add for Saturating<u32>
impl Add for Saturating<u32>
type Output = Saturating<u32>
1.74.0 · Source§impl Add for Saturating<u64>
impl Add for Saturating<u64>
type Output = Saturating<u64>
1.74.0 · Source§impl Add for Saturating<u128>
impl Add for Saturating<u128>
type Output = Saturating<u128>
1.74.0 · Source§impl Add for Saturating<usize>
impl Add for Saturating<usize>
type Output = Saturating<usize>
1.0.0 · Source§impl Add<&str> for String
Implements the +
operator for concatenating two strings.
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;