pub trait Add<Rhs = Self> {
type Output;
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
.
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 });
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,
}
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 });
The resulting type after applying the +
operator.
Performs the +
operation.
impl Add<LittleEndian<f32>> for LittleEndian<f32>
impl Add<LittleEndian<f64>> for LittleEndian<f64>
impl Add<LittleEndian<i16>> for LittleEndian<i16>
impl Add<LittleEndian<i32>> for LittleEndian<i32>
impl Add<LittleEndian<i64>> for LittleEndian<i64>
impl Add<LittleEndian<u16>> for LittleEndian<u16>
impl Add<LittleEndian<u32>> for LittleEndian<u32>
impl Add<LittleEndian<u64>> for LittleEndian<u64>
impl Add<NativeEndian<f32>> for NativeEndian<f32>
impl Add<NativeEndian<f64>> for NativeEndian<f64>
impl Add<NativeEndian<i16>> for NativeEndian<i16>
impl Add<NativeEndian<i32>> for NativeEndian<i32>
impl Add<NativeEndian<i64>> for NativeEndian<i64>
impl Add<NativeEndian<u16>> for NativeEndian<u16>
impl Add<NativeEndian<u32>> for NativeEndian<u32>
impl Add<NativeEndian<u64>> for NativeEndian<u64>
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
.
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;
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;
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;
impl<'_> Add<&'_ BigEndian<f32>> for BigEndian<f32>
impl<'_> Add<&'_ BigEndian<f64>> for BigEndian<f64>
impl<'_> Add<&'_ BigEndian<i16>> for BigEndian<i16>
impl<'_> Add<&'_ BigEndian<i32>> for BigEndian<i32>
impl<'_> Add<&'_ BigEndian<i64>> for BigEndian<i64>
impl<'_> Add<&'_ BigEndian<u16>> for BigEndian<u16>
impl<'_> Add<&'_ BigEndian<u32>> for BigEndian<u32>
impl<'_> Add<&'_ BigEndian<u64>> for BigEndian<u64>
impl<'_> Add<&'_ LittleEndian<f32>> for LittleEndian<f32>
impl<'_> Add<&'_ LittleEndian<f64>> for LittleEndian<f64>
impl<'_> Add<&'_ LittleEndian<i16>> for LittleEndian<i16>
impl<'_> Add<&'_ LittleEndian<i32>> for LittleEndian<i32>
impl<'_> Add<&'_ LittleEndian<i64>> for LittleEndian<i64>
impl<'_> Add<&'_ LittleEndian<i128>> for LittleEndian<i128>
impl<'_> Add<&'_ LittleEndian<u16>> for LittleEndian<u16>
impl<'_> Add<&'_ LittleEndian<u32>> for LittleEndian<u32>
impl<'_> Add<&'_ LittleEndian<u64>> for LittleEndian<u64>
impl<'_> Add<&'_ LittleEndian<u128>> for LittleEndian<u128>
impl<'_> Add<&'_ NativeEndian<f32>> for NativeEndian<f32>
impl<'_> Add<&'_ NativeEndian<f64>> for NativeEndian<f64>
impl<'_> Add<&'_ NativeEndian<i16>> for NativeEndian<i16>
impl<'_> Add<&'_ NativeEndian<i32>> for NativeEndian<i32>
impl<'_> Add<&'_ NativeEndian<i64>> for NativeEndian<i64>
impl<'_> Add<&'_ NativeEndian<i128>> for NativeEndian<i128>
impl<'_> Add<&'_ NativeEndian<u16>> for NativeEndian<u16>
impl<'_> Add<&'_ NativeEndian<u32>> for NativeEndian<u32>
impl<'_> Add<&'_ NativeEndian<u64>> for NativeEndian<u64>
impl<'_> Add<&'_ NativeEndian<u128>> for NativeEndian<u128>
impl<'_> Add<BigEndian<f32>> for &'_ BigEndian<f32>
impl<'_> Add<BigEndian<f64>> for &'_ BigEndian<f64>
impl<'_> Add<BigEndian<i16>> for &'_ BigEndian<i16>
impl<'_> Add<BigEndian<i32>> for &'_ BigEndian<i32>
impl<'_> Add<BigEndian<i64>> for &'_ BigEndian<i64>
impl<'_> Add<BigEndian<u16>> for &'_ BigEndian<u16>
impl<'_> Add<BigEndian<u32>> for &'_ BigEndian<u32>
impl<'_> Add<BigEndian<u64>> for &'_ BigEndian<u64>
impl<'_> Add<LittleEndian<f32>> for &'_ LittleEndian<f32>
impl<'_> Add<LittleEndian<f64>> for &'_ LittleEndian<f64>
impl<'_> Add<LittleEndian<i16>> for &'_ LittleEndian<i16>
impl<'_> Add<LittleEndian<i32>> for &'_ LittleEndian<i32>
impl<'_> Add<LittleEndian<i64>> for &'_ LittleEndian<i64>
impl<'_> Add<LittleEndian<i128>> for &'_ LittleEndian<i128>
impl<'_> Add<LittleEndian<u16>> for &'_ LittleEndian<u16>
impl<'_> Add<LittleEndian<u32>> for &'_ LittleEndian<u32>
impl<'_> Add<LittleEndian<u64>> for &'_ LittleEndian<u64>
impl<'_> Add<LittleEndian<u128>> for &'_ LittleEndian<u128>
impl<'_> Add<NativeEndian<f32>> for &'_ NativeEndian<f32>
impl<'_> Add<NativeEndian<f64>> for &'_ NativeEndian<f64>
impl<'_> Add<NativeEndian<i16>> for &'_ NativeEndian<i16>
impl<'_> Add<NativeEndian<i32>> for &'_ NativeEndian<i32>
impl<'_> Add<NativeEndian<i64>> for &'_ NativeEndian<i64>
impl<'_> Add<NativeEndian<i128>> for &'_ NativeEndian<i128>
impl<'_> Add<NativeEndian<u16>> for &'_ NativeEndian<u16>
impl<'_> Add<NativeEndian<u32>> for &'_ NativeEndian<u32>
impl<'_> Add<NativeEndian<u64>> for &'_ NativeEndian<u64>
impl<'_> Add<NativeEndian<u128>> for &'_ NativeEndian<u128>
impl<'_, '_> Add<&'_ f32> for &'_ BigEndian<f32>
impl<'_, '_> Add<&'_ f32> for &'_ LittleEndian<f32>
impl<'_, '_> Add<&'_ f32> for &'_ NativeEndian<f32>
impl<'_, '_> Add<&'_ f64> for &'_ BigEndian<f64>
impl<'_, '_> Add<&'_ f64> for &'_ LittleEndian<f64>
impl<'_, '_> Add<&'_ f64> for &'_ NativeEndian<f64>
impl<'_, '_> Add<&'_ i16> for &'_ BigEndian<i16>
impl<'_, '_> Add<&'_ i16> for &'_ LittleEndian<i16>
impl<'_, '_> Add<&'_ i16> for &'_ NativeEndian<i16>
impl<'_, '_> Add<&'_ i32> for &'_ BigEndian<i32>
impl<'_, '_> Add<&'_ i32> for &'_ LittleEndian<i32>
impl<'_, '_> Add<&'_ i32> for &'_ NativeEndian<i32>
impl<'_, '_> Add<&'_ i64> for &'_ BigEndian<i64>
impl<'_, '_> Add<&'_ i64> for &'_ LittleEndian<i64>
impl<'_, '_> Add<&'_ i64> for &'_ NativeEndian<i64>
impl<'_, '_> Add<&'_ u16> for &'_ BigEndian<u16>
impl<'_, '_> Add<&'_ u16> for &'_ LittleEndian<u16>
impl<'_, '_> Add<&'_ u16> for &'_ NativeEndian<u16>
impl<'_, '_> Add<&'_ u32> for &'_ BigEndian<u32>
impl<'_, '_> Add<&'_ u32> for &'_ LittleEndian<u32>
impl<'_, '_> Add<&'_ u32> for &'_ NativeEndian<u32>
impl<'_, '_> Add<&'_ u64> for &'_ BigEndian<u64>
impl<'_, '_> Add<&'_ u64> for &'_ LittleEndian<u64>
impl<'_, '_> Add<&'_ u64> for &'_ NativeEndian<u64>
impl<'_, '_> Add<&'_ BigEndian<f32>> for &'_ f32
impl<'_, '_> Add<&'_ BigEndian<f32>> for &'_ BigEndian<f32>
impl<'_, '_> Add<&'_ BigEndian<f64>> for &'_ f64
impl<'_, '_> Add<&'_ BigEndian<f64>> for &'_ BigEndian<f64>
impl<'_, '_> Add<&'_ BigEndian<i16>> for &'_ i16
impl<'_, '_> Add<&'_ BigEndian<i16>> for &'_ BigEndian<i16>
impl<'_, '_> Add<&'_ BigEndian<i32>> for &'_ i32
impl<'_, '_> Add<&'_ BigEndian<i32>> for &'_ BigEndian<i32>
impl<'_, '_> Add<&'_ BigEndian<i64>> for &'_ i64
impl<'_, '_> Add<&'_ BigEndian<i64>> for &'_ BigEndian<i64>
impl<'_, '_> Add<&'_ BigEndian<i128>> for &'_ BigEndian<i128>
impl<'_, '_> Add<&'_ BigEndian<u16>> for &'_ u16
impl<'_, '_> Add<&'_ BigEndian<u16>> for &'_ BigEndian<u16>
impl<'_, '_> Add<&'_ BigEndian<u32>> for &'_ u32
impl<'_, '_> Add<&'_ BigEndian<u32>> for &'_ BigEndian<u32>
impl<'_, '_> Add<&'_ BigEndian<u64>> for &'_ u64
impl<'_, '_> Add<&'_ BigEndian<u64>> for &'_ BigEndian<u64>
impl<'_, '_> Add<&'_ BigEndian<u128>> for &'_ BigEndian<u128>
impl<'_, '_> Add<&'_ LittleEndian<f32>> for &'_ f32
impl<'_, '_> Add<&'_ LittleEndian<f32>> for &'_ LittleEndian<f32>
impl<'_, '_> Add<&'_ LittleEndian<f64>> for &'_ f64
impl<'_, '_> Add<&'_ LittleEndian<f64>> for &'_ LittleEndian<f64>
impl<'_, '_> Add<&'_ LittleEndian<i16>> for &'_ i16
impl<'_, '_> Add<&'_ LittleEndian<i16>> for &'_ LittleEndian<i16>
impl<'_, '_> Add<&'_ LittleEndian<i32>> for &'_ i32
impl<'_, '_> Add<&'_ LittleEndian<i32>> for &'_ LittleEndian<i32>
impl<'_, '_> Add<&'_ LittleEndian<i64>> for &'_ i64
impl<'_, '_> Add<&'_ LittleEndian<i64>> for &'_ LittleEndian<i64>
impl<'_, '_> Add<&'_ LittleEndian<i128>> for &'_ LittleEndian<i128>
impl<'_, '_> Add<&'_ LittleEndian<u16>> for &'_ u16
impl<'_, '_> Add<&'_ LittleEndian<u16>> for &'_ LittleEndian<u16>
impl<'_, '_> Add<&'_ LittleEndian<u32>> for &'_ u32
impl<'_, '_> Add<&'_ LittleEndian<u32>> for &'_ LittleEndian<u32>
impl<'_, '_> Add<&'_ LittleEndian<u64>> for &'_ u64
impl<'_, '_> Add<&'_ LittleEndian<u64>> for &'_ LittleEndian<u64>
impl<'_, '_> Add<&'_ LittleEndian<u128>> for &'_ LittleEndian<u128>
impl<'_, '_> Add<&'_ NativeEndian<f32>> for &'_ f32
impl<'_, '_> Add<&'_ NativeEndian<f32>> for &'_ NativeEndian<f32>
impl<'_, '_> Add<&'_ NativeEndian<f64>> for &'_ f64
impl<'_, '_> Add<&'_ NativeEndian<f64>> for &'_ NativeEndian<f64>
impl<'_, '_> Add<&'_ NativeEndian<i16>> for &'_ i16
impl<'_, '_> Add<&'_ NativeEndian<i16>> for &'_ NativeEndian<i16>
impl<'_, '_> Add<&'_ NativeEndian<i32>> for &'_ i32
impl<'_, '_> Add<&'_ NativeEndian<i32>> for &'_ NativeEndian<i32>
impl<'_, '_> Add<&'_ NativeEndian<i64>> for &'_ i64
impl<'_, '_> Add<&'_ NativeEndian<i64>> for &'_ NativeEndian<i64>
impl<'_, '_> Add<&'_ NativeEndian<i128>> for &'_ NativeEndian<i128>
impl<'_, '_> Add<&'_ NativeEndian<u16>> for &'_ u16
impl<'_, '_> Add<&'_ NativeEndian<u16>> for &'_ NativeEndian<u16>
impl<'_, '_> Add<&'_ NativeEndian<u32>> for &'_ u32
impl<'_, '_> Add<&'_ NativeEndian<u32>> for &'_ NativeEndian<u32>
impl<'_, '_> Add<&'_ NativeEndian<u64>> for &'_ u64
impl<'_, '_> Add<&'_ NativeEndian<u64>> for &'_ NativeEndian<u64>
impl<'_, '_> Add<&'_ NativeEndian<u128>> for &'_ NativeEndian<u128>