Trait RangeExt

Source
pub trait RangeExt {
    // Required methods
    fn concat(&self, other: &Self) -> Self
       where Self: Clone;
    fn remove_prefix(self, prefix: Self) -> Self;
    fn remove_suffix(self, suffix: Self) -> Self;

    // Provided methods
    fn cut<C>(&self, middle: &C) -> (Self, Self)
       where Self: Sized + RangeCut<C> { ... }
    fn compose<Rhs, Output>(&self, rhs: &Rhs) -> Output
       where Self: RangeCompose<Rhs, Output = Output> { ... }
}

Required Methods§

Source

fn concat(&self, other: &Self) -> Self
where Self: Clone,

Concatenate self and other, panicking if other doesn’t immediately follow self.

assert_eq!((0..3).concat(&(3..4)), 0..4);

let arr = [0, 1, 2, 3, 4];

assert_eq!([0, 1, 2      ], arr[0..3]);
assert_eq!([         3   ], arr[3..4]);
assert_eq!([0, 1, 2, 3   ], arr[(0..3).concat(&(3..4))]);
Source

fn remove_prefix(self, prefix: Self) -> Self

Remove prefix from self, panicking if it isn’t a prefix of self.

assert_eq!((0..5).remove_prefix(0..1), 1..5);

let arr = [0, 1, 2];

assert_eq!([0, 1, 2], arr[0..3]);
assert_eq!([0      ], arr[0..1]);
assert_eq!([   1, 2], arr[(0..3).remove_prefix(0..1)]);
Source

fn remove_suffix(self, suffix: Self) -> Self

Remove suffix from self, panicking if it isn’t a suffix of self.

assert_eq!((0..5).remove_suffix(3..5), 0..3);

let arr = [0, 1, 2];

assert_eq!([0, 1, 2], arr[0..3]);
assert_eq!([      2], arr[2..3]);
assert_eq!([0, 1   ], arr[(0..3).remove_suffix(2..3)]);

Provided Methods§

Source

fn cut<C>(&self, middle: &C) -> (Self, Self)
where Self: Sized + RangeCut<C>,

Split the range into the section before middle starts, and the section that starts where middle ends. Panics if middle contains any elements not in self.

assert_eq!((0..5).cut(&(1..3)), (0..1, 3..5));

let arr = [0, 1, 2, 3, 4];

let middle = 1..3;
let (before, after) = (0..5).cut(&middle);

assert_eq!(
    arr[before]
        .into_iter()
        .chain(arr[after].into_iter())
        .copied()
        .collect::<Vec<_>>(),
    [0,       3, 4],
);

assert_eq!(
    arr[middle],
    [   1, 2      ],
);
Source

fn compose<Rhs, Output>(&self, rhs: &Rhs) -> Output
where Self: RangeCompose<Rhs, Output = Output>,

Calculate the range such that indexing by the new range has the same result as indexing by self and then rhs.

let arr = [0, 1, 2, 3, 4];

let outer = 2..4;
let inner = 1..2;

assert_eq!(arr[outer.compose(&inner)], arr[outer][inner]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> RangeExt for Range<T>

Source§

fn concat(&self, other: &Self) -> Self

Source§

fn remove_prefix(self, prefix: Self) -> Self

Source§

fn remove_suffix(self, suffix: Self) -> Self

Implementors§