Expand description
The subtraction operator -
.
Note that Rhs
is Self
by default, but this is not mandatory. For
example, std::time::SystemTime
implements Sub<Duration>
, which permits
operations of the form SystemTime = SystemTime - Duration
.
Examples
Sub
tractable points
use std::ops::Sub;
#[derive(Debug, Copy, Clone, PartialEq)]
struct Point {
x: i32,
y: i32,
}
impl Sub for Point {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Self {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
Point { x: 1, y: 0 });
Implementing Sub
with generics
Here is an example of the same Point
struct implementing the Sub
trait
using generics.
use std::ops::Sub;
#[derive(Debug, PartialEq)]
struct Point<T> {
x: T,
y: T,
}
// Notice that the implementation uses the associated type `Output`.
impl<T: Sub<Output = T>> Sub for Point<T> {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Point {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
assert_eq!(Point { x: 2, y: 3 } - Point { x: 1, y: 0 },
Point { x: 1, y: 3 });
Associated Types
Required methods
Implementations on Foreign Types
sourceimpl<'_, '_, T, S> Sub<&'_ HashSet<T, S>> for &'_ HashSet<T, S> where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
impl<'_, '_, T, S> Sub<&'_ HashSet<T, S>> for &'_ HashSet<T, S> where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
sourcefn sub(self, rhs: &HashSet<T, S>) -> HashSet<T, S>
fn sub(self, rhs: &HashSet<T, S>) -> HashSet<T, S>
Returns the difference of self
and rhs
as a new HashSet<T, S>
.
Examples
use std::collections::HashSet;
let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([3, 4, 5]);
let set = &a - &b;
let mut i = 0;
let expected = [1, 2];
for x in &set {
assert!(expected.contains(x));
i += 1;
}
assert_eq!(i, expected.len());
type Output = HashSet<T, S>
1.8.0 · sourceimpl Sub<Duration> for SystemTime
impl Sub<Duration> for SystemTime
type Output = SystemTime
fn sub(self, dur: Duration) -> SystemTime
1.8.0 · sourceimpl Sub<Instant> for Instant
impl Sub<Instant> for Instant
sourcefn sub(self, other: Instant) -> Duration
fn sub(self, other: Instant) -> Duration
Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.
Panics
Previous rust versions panicked when other
was later than self
. Currently this
method saturates. Future versions may reintroduce the panic in some circumstances.
See Monotonicity.
type Output = Duration
sourceimpl<'_, '_, T> Sub<&'_ BTreeSet<T>> for &'_ BTreeSet<T> where
T: Ord + Clone,
impl<'_, '_, T> Sub<&'_ BTreeSet<T>> for &'_ BTreeSet<T> where
T: Ord + Clone,
sourcefn sub(self, rhs: &BTreeSet<T>) -> BTreeSet<T>
fn sub(self, rhs: &BTreeSet<T>) -> BTreeSet<T>
Returns the difference of self
and rhs
as a new BTreeSet<T>
.
Examples
use std::collections::BTreeSet;
let a = BTreeSet::from([1, 2, 3]);
let b = BTreeSet::from([3, 4, 5]);
let result = &a - &b;
let result_vec: Vec<_> = result.into_iter().collect();
assert_eq!(result_vec, [1, 2]);
type Output = BTreeSet<T>
sourceimpl<'_, '_, T, S1, S2> Sub<&'_ IndexSet<T, S2>> for &'_ IndexSet<T, S1> where
T: Eq + Hash + Clone,
S1: BuildHasher + Default,
S2: BuildHasher,
impl<'_, '_, T, S1, S2> Sub<&'_ IndexSet<T, S2>> for &'_ IndexSet<T, S1> where
T: Eq + Hash + Clone,
S1: BuildHasher + Default,
S2: BuildHasher,
sourceimpl<'_, '_, T, S> Sub<&'_ HashSet<T, S, Global>> for &'_ HashSet<T, S, Global> where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
impl<'_, '_, T, S> Sub<&'_ HashSet<T, S, Global>> for &'_ HashSet<T, S, Global> where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
sourcefn sub(self, rhs: &HashSet<T, S, Global>) -> HashSet<T, S, Global>
fn sub(self, rhs: &HashSet<T, S, Global>) -> HashSet<T, S, Global>
Returns the difference of self
and rhs
as a new HashSet<T, S>
.
Examples
use hashbrown::HashSet;
let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
let set = &a - &b;
let mut i = 0;
let expected = [1, 2];
for x in &set {
assert!(expected.contains(x));
i += 1;
}
assert_eq!(i, expected.len());