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.
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<f32>> for LittleEndian<f32>
impl Add<&LittleEndian<f64>> for &LittleEndian<f64>
impl Add<&LittleEndian<f64>> for LittleEndian<f64>
impl Add<&LittleEndian<i16>> for &LittleEndian<i16>
impl Add<&LittleEndian<i16>> for LittleEndian<i16>
impl Add<&LittleEndian<i32>> for &LittleEndian<i32>
impl Add<&LittleEndian<i32>> for LittleEndian<i32>
impl Add<&LittleEndian<i64>> for &LittleEndian<i64>
impl Add<&LittleEndian<i64>> for LittleEndian<i64>
impl Add<&LittleEndian<u16>> for &LittleEndian<u16>
impl Add<&LittleEndian<u16>> for LittleEndian<u16>
impl Add<&LittleEndian<u32>> for &LittleEndian<u32>
impl Add<&LittleEndian<u32>> for LittleEndian<u32>
impl Add<&LittleEndian<u64>> for &LittleEndian<u64>
impl Add<&LittleEndian<u64>> for LittleEndian<u64>
impl Add<&NativeEndian<f32>> for &NativeEndian<f32>
impl Add<&NativeEndian<f32>> for NativeEndian<f32>
impl Add<&NativeEndian<f64>> for &NativeEndian<f64>
impl Add<&NativeEndian<f64>> for NativeEndian<f64>
impl Add<&NativeEndian<i16>> for &NativeEndian<i16>
impl Add<&NativeEndian<i16>> for NativeEndian<i16>
impl Add<&NativeEndian<i32>> for &NativeEndian<i32>
impl Add<&NativeEndian<i32>> for NativeEndian<i32>
impl Add<&NativeEndian<i64>> for &NativeEndian<i64>
impl Add<&NativeEndian<i64>> for NativeEndian<i64>
impl Add<&NativeEndian<u16>> for &NativeEndian<u16>
impl Add<&NativeEndian<u16>> for NativeEndian<u16>
impl Add<&NativeEndian<u32>> for &NativeEndian<u32>
impl Add<&NativeEndian<u32>> for NativeEndian<u32>
impl Add<&NativeEndian<u64>> for &NativeEndian<u64>
impl Add<&NativeEndian<u64>> for NativeEndian<u64>
impl Add<LittleEndian<f32>> for &LittleEndian<f32>
impl Add<LittleEndian<f32>> for LittleEndian<f32>
impl Add<LittleEndian<f64>> for &LittleEndian<f64>
impl Add<LittleEndian<f64>> for LittleEndian<f64>
impl Add<LittleEndian<i16>> for &LittleEndian<i16>
impl Add<LittleEndian<i16>> for LittleEndian<i16>
impl Add<LittleEndian<i32>> for &LittleEndian<i32>
impl Add<LittleEndian<i32>> for LittleEndian<i32>
impl Add<LittleEndian<i64>> for &LittleEndian<i64>
impl Add<LittleEndian<i64>> for LittleEndian<i64>
impl Add<LittleEndian<u16>> for &LittleEndian<u16>
impl Add<LittleEndian<u16>> for LittleEndian<u16>
impl Add<LittleEndian<u32>> for &LittleEndian<u32>
impl Add<LittleEndian<u32>> for LittleEndian<u32>
impl Add<LittleEndian<u64>> for &LittleEndian<u64>
impl Add<LittleEndian<u64>> for LittleEndian<u64>
impl Add<NativeEndian<f32>> for &NativeEndian<f32>
impl Add<NativeEndian<f32>> for NativeEndian<f32>
impl Add<NativeEndian<f64>> for &NativeEndian<f64>
impl Add<NativeEndian<f64>> for NativeEndian<f64>
impl Add<NativeEndian<i16>> for &NativeEndian<i16>
impl Add<NativeEndian<i16>> for NativeEndian<i16>
impl Add<NativeEndian<i32>> for &NativeEndian<i32>
impl Add<NativeEndian<i32>> for NativeEndian<i32>
impl Add<NativeEndian<i64>> for &NativeEndian<i64>
impl Add<NativeEndian<i64>> for NativeEndian<i64>
impl Add<NativeEndian<u16>> for &NativeEndian<u16>
impl Add<NativeEndian<u16>> for NativeEndian<u16>
impl Add<NativeEndian<u32>> for &NativeEndian<u32>
impl Add<NativeEndian<u32>> for NativeEndian<u32>
impl Add<NativeEndian<u64>> for &NativeEndian<u64>
impl Add<NativeEndian<u64>> for NativeEndian<u64>
sourceimpl<T> Add<T> for Pages where
T: Into<Self>,