Trait TakeSlice

Source
pub trait TakeSlice<T: Clone> {
    // Required methods
    fn as_slice(&self) -> &[T];
    fn take(self) -> Vec<T>;
}
Expand description

The TakeSlice trait for Rust enables treating slices and Vec instances interchangeably.

§Examples

take_slice can take in both slices and vectors. If it takes in a slice, no copying is needed unless take is called.

use take_ref::TakeSlice;
 
fn take_slice(value: impl TakeSlice<i64>) {
    assert_eq!(value.as_slice(), [1, 2, 3]); // `as_slice` references the value in place.
    assert_eq!(value.as_slice(), [1, 2, 3]); // `as_slice` can be repeated until `take`.
    assert_eq!(value.take(), [1, 2, 3]); // `take` consumes the value.
}
 
let numbers = vec!(1, 2, 3);
take_slice(numbers.as_slice());
take_slice(numbers);

§Disallowed Operations

slice_taken fails to compile since it attempts to slice value after take has already consumed it.

fn slice_taken(value: impl take_ref::TakeSlice<i64>) {
    value.take(); // `take` consumes the value.
    value.slice(); // This call is disallowed since the value has been consumed.
}

take_taken fails to compile since it attempts to take value after take has already consumed it.

fn take_taken(value: impl take_ref::TakeSlice<i64>) {
    value.take(); // `take` consumes the value.
    value.take(); // This call is disallowed since the value has been consumed.
}

Required Methods§

Source

fn as_slice(&self) -> &[T]

Reference the value as a slice.

Source

fn take(self) -> Vec<T>

Take ownership of the value or construct the desired value and drop self.

Implementations on Foreign Types§

Source§

impl<'a, T: Clone> TakeSlice<T> for &'a [T]

Source§

fn as_slice(&self) -> &[T]

Reference the value as a slice.

Source§

fn take(self) -> Vec<T>

Construct the desired value and drop self.

Source§

impl<T: Clone> TakeSlice<T> for Vec<T>

Source§

fn as_slice<'a>(&self) -> &[T]

Reference the value as a slice.

Source§

fn take(self) -> Vec<T>

Construct the desired value and drop self.

Implementors§