logo

Trait rocket::mtls::oid::asn1_rs::nom::lib::std::ops::Add

1.0.0 · source · []
pub trait Add<Rhs = Self> {
    type Output;

    fn add(self, rhs: Rhs) -> Self::Output;
}
Available on crate feature mtls only.
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

Addable 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

The resulting type after applying the + operator.

Required Methods

Performs the + operation.

Example
assert_eq!(12 + 1, 13);

Implementations on Foreign Types

Panics

This function may panic if the resulting point in time cannot be represented by the underlying data structure. See Instant::checked_add for a version without panic.

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.

Add the sub-day time of the Duration to the Time. Wraps on overflow.

assert_eq!(time!(12:00) + 2.hours(), time!(14:00));
assert_eq!(time!(0:00:01) + (-2).seconds(), time!(23:59:59));

Add the sub-day time of the std::time::Duration to the Time. Wraps on overflow.

assert_eq!(time!(12:00) + 2.std_hours(), time!(14:00));
assert_eq!(time!(23:59:59) + 2.std_seconds(), time!(0:00:01));

NInt + Z0 = NInt

UInt<U, B> + UTerm = UInt<U, B>

P(Ul) + N(Ur): We resolve this with our PrivateAdd

Z0 + I = I

PInt + Z0 = PInt

UTerm + U = U

P(Ul) + P(Ur) = P(Ul + Ur)

UInt<Ul, B1> + UInt<Ur, B0> = UInt<Ul + Ur, B1>

U + B0 = U

UInt<Ul, B0> + UInt<Ur, B0> = UInt<Ul + Ur, B0>

UTerm + B0 = UTerm

UTerm + B1 = UInt<UTerm, B1>

UInt<Ul, B1> + UInt<Ur, B1> = UInt<(Ul + Ur) + B1, B0>

N(Ul) + N(Ur) = N(Ul + Ur)

UInt<U, B1> + B1 = UInt<U + B1, B0>

UInt<U, B0> + B1 = UInt<U + B1>

UInt<Ul, B0> + UInt<Ur, B1> = UInt<Ul + Ur, B1>

N(Ul) + P(Ur): We resolve this with our PrivateAdd

Implementors

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 Strings 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;