pub trait SliceExt {
    type Item;

    fn map<'a, B, F>(&'a self, f: F) -> Vec<B>
    where
        F: FnMut(&'a Self::Item) -> B
; fn try_map<'a, B, E, F>(&'a self, f: F) -> Result<Vec<B>, E>
    where
        F: FnMut(&'a Self::Item) -> Result<B, E>
; fn as_singleton(&self) -> Option<&Self::Item>; fn dupe_from_slice(&mut self, src: &[Self::Item])
    where
        Self::Item: Dupe
; fn owned<'a, T, R>(&'a self) -> Vec<R>
    where
        Self::Item: TEq<&'a T>,
        R: Borrow<T>,
        T: ToOwned<Owned = R>,
        T: 'a,
        T: ?Sized
, { ... } }
Expand description

Extension traits on slices/Vec.

Required Associated Types

Required Methods

A shorthand for iter().map(f).collect::<Vec<_>>(). For example:

use gazebo::prelude::*;
assert_eq!([1,2,3][..].map(|x| x*x), vec![1,4,9]);
assert_eq!(vec![1,2,3].map(|x| x*x), vec![1,4,9]);

Note that from Rust 1.55.0 there is a map method on arrays (e.g. [T; N]) so you’ll need to explicitly convert arrays to slices with the [..] operation.

A shorthand for iter().map(f).collect::<Result<Vec<_>, _>>(). For example:

use gazebo::prelude::*;
assert_eq!([1,2,3].try_map(|x| Ok(x*x)), Ok::<_, bool>(vec![1,4,9]));
assert_eq!([1,2,-3].try_map(|x| if *x > 0 { Ok(x*x) } else { Err(false) }), Err(false));

This function will be generalised to Try once it has been standardised.

If the size of vector is 1, returns the first element Otherwise, returns None

use gazebo::prelude::*;
assert_eq!(*vec![1].as_singleton().unwrap(), 1);
assert_eq!(vec!['a', 'b', 'c'].as_singleton(), None);

Copies the elements from src into self, analogous to clone_from_slice but for elements that are dupe.

The length of src must be the same as self.

If T implements Copy, it can be more performant to use [std::slice::[T]::copy_from_slice].

use gazebo::prelude::*;

let src = [1, 2, 3, 4];
let mut dst = [0, 0];

dst.dupe_from_slice(&src[2..]);
assert_eq!(src, [1, 2, 3, 4]);
assert_eq!(dst, [3, 4]);

Provided Methods

Take ownership of each item in the vector using to_owned. For example:

use gazebo::prelude::*;
let xs: &[&str] = &["hello", "world"];
let ys: Vec<String> = xs.owned();

Implementations on Foreign Types

Implementors