Trait core_extensions::slices::ValSliceExt

source ·
pub trait ValSliceExt: SliceExt + Borrow<[<Self as SliceExt>::Elem]> {
    // Provided methods
    fn split_while<'a, P, U>(
        &'a self,
        mapper: P,
    ) -> SplitSliceWhile<'a, Self::Elem, P, U> 
       where P: FnMut(&'a Self::Elem) -> U,
             U: Eq + Clone { ... }
    fn rsplit_while<'a, P, U>(
        &'a self,
        mapper: P,
    ) -> RSplitSliceWhile<'a, Self::Elem, P, U> 
       where P: FnMut(&'a Self::Elem) -> U,
             U: Eq + Clone { ... }
}
Expand description

Extension trait for [T].

Provided Methods§

source

fn split_while<'a, P, U>( &'a self, mapper: P, ) -> SplitSliceWhile<'a, Self::Elem, P, U>
where P: FnMut(&'a Self::Elem) -> U, U: Eq + Clone,

Returns an iterator over subslices whose elements were mapped to the same value by mapper.

The returned type implements DoubleEndedIterator<Item =KeySlice<Self::Elem, U>>.

§Example
use core_extensions::ValSliceExt;
use core_extensions::slices::KeySlice;
 
{
    let list = vec![0, 1, 2, 3, 4, 5, 6, 7, 8];
 
    assert_eq!(
        list.split_while(|x| x/4).collect::<Vec<_>>(),
        vec![
            KeySlice{key: 0, slice: &[0, 1, 2, 3]},
            KeySlice{key: 1, slice: &[4, 5, 6, 7]},
            KeySlice{key: 2, slice: &[8]},
        ]
    );
}
 
{
    let list = [0, 4, 1, 5, 9, 8, 7];
 
    assert_eq!(
        list.split_while(|x| x%4).collect::<Vec<_>>(),
        vec![
            KeySlice{key: 0, slice: &[0, 4]},
            KeySlice{key: 1, slice: &[1, 5, 9]},
            KeySlice{key: 0, slice: &[8]},
            KeySlice{key: 3, slice: &[7]},
        ]
    );
}
 
 
source

fn rsplit_while<'a, P, U>( &'a self, mapper: P, ) -> RSplitSliceWhile<'a, Self::Elem, P, U>
where P: FnMut(&'a Self::Elem) -> U, U: Eq + Clone,

A variation of split_while that iterates from the right(the order of subslices is reversed).

The returned type implements DoubleEndedIterator<Item =KeySlice<Self::Elem, U>>.

§Example
use core_extensions::ValSliceExt;
use core_extensions::slices::KeySlice;
 
{
    let list = vec![0, 1, 2, 3, 4, 5, 6, 7, 8];
 
    assert_eq!(
        list.rsplit_while(|x| x/4 ).collect::<Vec<_>>(),
        vec![
            KeySlice{key: 2, slice: &[8]},
            KeySlice{key: 1, slice: &[4, 5, 6, 7]},
            KeySlice{key: 0, slice: &[0, 1, 2, 3]},
        ]
    );
}
 
{
    let list = [0, 4, 1, 5, 9, 8, 7];
 
    assert_eq!(
        list.rsplit_while(|x| x%4 ).collect::<Vec<_>>(),
        vec![
            KeySlice{key: 3, slice: &[7]},
            KeySlice{key: 0, slice: &[8]},
            KeySlice{key: 1, slice: &[1, 5, 9]},
            KeySlice{key: 0, slice: &[0, 4]},
        ]
    );
}
 
 
 

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<This> ValSliceExt for This
where This: ?Sized + SliceExt + Borrow<[Self::Elem]>,