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 });
RunImplementing 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 });
RunRequired Associated Types
Required Methods
Implementors
sourceimpl Add<Saturating<i8>> for Saturating<i8>
impl Add<Saturating<i8>> for Saturating<i8>
type Output = Saturating<i8>
sourceimpl Add<Saturating<i16>> for Saturating<i16>
impl Add<Saturating<i16>> for Saturating<i16>
type Output = Saturating<i16>
sourceimpl Add<Saturating<i32>> for Saturating<i32>
impl Add<Saturating<i32>> for Saturating<i32>
type Output = Saturating<i32>
sourceimpl Add<Saturating<i64>> for Saturating<i64>
impl Add<Saturating<i64>> for Saturating<i64>
type Output = Saturating<i64>
sourceimpl Add<Saturating<i128>> for Saturating<i128>
impl Add<Saturating<i128>> for Saturating<i128>
type Output = Saturating<i128>
sourceimpl Add<Saturating<isize>> for Saturating<isize>
impl Add<Saturating<isize>> for Saturating<isize>
type Output = Saturating<isize>
sourceimpl Add<Saturating<u8>> for Saturating<u8>
impl Add<Saturating<u8>> for Saturating<u8>
type Output = Saturating<u8>
sourceimpl Add<Saturating<u16>> for Saturating<u16>
impl Add<Saturating<u16>> for Saturating<u16>
type Output = Saturating<u16>
sourceimpl Add<Saturating<u32>> for Saturating<u32>
impl Add<Saturating<u32>> for Saturating<u32>
type Output = Saturating<u32>
sourceimpl Add<Saturating<u64>> for Saturating<u64>
impl Add<Saturating<u64>> for Saturating<u64>
type Output = Saturating<u64>
sourceimpl Add<Saturating<u128>> for Saturating<u128>
impl Add<Saturating<u128>> for Saturating<u128>
type Output = Saturating<u128>
sourceimpl Add<Saturating<usize>> for Saturating<usize>
impl Add<Saturating<usize>> for Saturating<usize>
type Output = Saturating<usize>
1.8.0 · sourceimpl Add<Duration> for SystemTime
impl Add<Duration> for SystemTime
type Output = SystemTime
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.
RunIf 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.
RunConcatenating &str
slices can be done by converting the first to a String
:
let a = "hello";
let b = " world";
let c = a.to_string() + b;
Run