Trait SliceBorrowed

Source
pub trait SliceBorrowed: Slice {
    // Required method
    fn get(&self, index: usize) -> Option<&Self::Output>;

    // Provided methods
    fn array_chunks<const N: usize>(&self) -> ArrayChunksBorrowed<'_, Self, N>  { ... }
    fn array_windows<const N: usize>(&self) -> ArrayWindowsBorrowed<'_, Self, N>  { ... }
    fn chunks(&self, size: usize) -> ChunksBorrowed<'_, Self>  { ... }
    fn map<F: Fn(&Self::Output) -> R, R>(self, f: F) -> MapBorrowed<Self, F>
       where Self: Sized { ... }
    fn cloned(self) -> Cloned<Self>
       where Self: Sized,
             Self::Output: Clone { ... }
    fn iter(&self) -> IterBorrowed<'_, Self>  { ... }
    fn windows(&self, size: usize) -> WindowsBorrowed<'_, Self>  { ... }
}
Expand description

A Slice that can return borrowed values.

Required Methods§

Source

fn get(&self, index: usize) -> Option<&Self::Output>

Index the slice, returning a borrowed value.

Provided Methods§

Source

fn array_chunks<const N: usize>(&self) -> ArrayChunksBorrowed<'_, Self, N>

Return an iterator over arrays covering consecutive portions of the slice.

Analagous to [slice::array_chunks].

§Panics

If N == 0, panics.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut iter = slice.array_chunks::<2>();

assert_eq!(iter.next(), Some([&1, &2]));
assert_eq!(iter.next(), Some([&3, &4]));
assert!(iter.next().is_none());
assert_eq!(iter.remainder(), [5]);
Source

fn array_windows<const N: usize>(&self) -> ArrayWindowsBorrowed<'_, Self, N>

Return a slice/iterator over arrays covering overlapping portions of the slice.

Analagous to slice::array_windows.

§Panics

If N == 0, panics.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut windows = slice.array_windows::<3>();

assert_eq!(windows.next(), Some([&1, &2, &3]));
assert_eq!(windows.next(), Some([&2, &3, &4]));
assert_eq!(windows.next(), Some([&3, &4, &5]));
assert!(windows.next().is_none());
assert_eq!(windows.get_owned(1), Some([&2, &3, &4]));
Source

fn chunks(&self, size: usize) -> ChunksBorrowed<'_, Self>

Return an iterator over slices covering consecutive portions of the slice.

Analagous to slice::chunks.

§Panics

If size == 0, panics.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut iter = slice.chunks(2);

assert_eq!(iter.next().unwrap(), [1, 2]);
assert_eq!(iter.next().unwrap(), [3, 4]);
assert_eq!(iter.next().unwrap(), [5]);
assert!(iter.next().is_none());
Source

fn map<F: Fn(&Self::Output) -> R, R>(self, f: F) -> MapBorrowed<Self, F>
where Self: Sized,

Call a closure on index, returning a new type.

Analagous to Iterator::map.

§Examples
let slice = [0, 1, 2].map(|x| x != 0);
assert_eq!(slice, [false, true, true]);
Source

fn cloned(self) -> Cloned<Self>
where Self: Sized, Self::Output: Clone,

Create a new slice that clones each value on access. Analagous to self.map(Clone::clone).

§Examples
struct Foo(u32);
impl Clone for Foo {
    fn clone(&self) -> Foo { Foo(self.0 + 1) }
}

let slice = [Foo(1), Foo(2)];
assert_eq!(slice.cloned(), [Foo(2), Foo(3)]);
Source

fn iter(&self) -> IterBorrowed<'_, Self>

Creates an iterator over the slice.

§Examples
let slice = [1, 2].chain([3]);
let mut iter = slice.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert!(iter.next().is_none());
Source

fn windows(&self, size: usize) -> WindowsBorrowed<'_, Self>

Return a slice/iterator over slices covering overlapping portions of the slice.

Analagous to slice::windows.

§Panics

If size == 0, panics.

§Examples
let slice = [1, 2, 3, 4, 5];
let mut windows = slice.windows(3);

assert_eq!(windows.next().unwrap(), [1, 2, 3]);
assert_eq!(windows.next().unwrap(), [2, 3, 4]);
assert_eq!(windows.next().unwrap(), [3, 4, 5]);
assert!(windows.next().is_none());
assert_eq!(windows.get_owned(1).unwrap(), [2, 3, 4]);

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<'a, S> SliceBorrowed for &'a S
where S: SliceBorrowed + ?Sized,

Source§

fn get(&self, index: usize) -> Option<&Self::Output>

Source§

impl<'a, S> SliceBorrowed for &'a mut S
where S: SliceBorrowed + ?Sized,

Source§

fn get(&self, index: usize) -> Option<&Self::Output>

Source§

impl<T> SliceBorrowed for [T]

Source§

fn get(&self, index: usize) -> Option<&Self::Output>

Source§

impl<T, const N: usize> SliceBorrowed for [T; N]

Source§

fn get(&self, index: usize) -> Option<&Self::Output>

Implementors§

Source§

impl<A> SliceBorrowed for SliceOf<A>
where A: SliceBorrowed,

Source§

impl<A> SliceBorrowed for SplitMut<'_, A>
where A: SliceBorrowed + ?Sized,

Source§

impl<S1, S2> SliceBorrowed for Chain<S1, S2>
where S1: SliceBorrowed, S2: SliceBorrowed<Output = S1::Output>,

Source§

impl<S1, S2> SliceBorrowed for Interleave<S1, S2>
where S1: SliceBorrowed, S2: SliceBorrowed<Output = S1::Output>,

Source§

impl<S> SliceBorrowed for Cycle<S>
where S: SliceBorrowed,

Source§

impl<S> SliceBorrowed for Reverse<S>
where S: SliceBorrowed,