Function partition::partition [−][src]
pub fn partition<T, P>(data: &mut [T], predicate: P) -> (&mut [T], &mut [T]) where
P: Fn(&T) -> bool,
partition a mutable slice in-place so that it contains all elements for
which predicate(e)
is true
, followed by all elements for which
predicate(e)
is false
. Returns sub-slices to all predicated and
non-predicated elements, respectively.
This does roughly the same as Iterator::partition(_)
, but without
requiring any additional storage.
Examples
let mut even_odd = [0u8, 1, 2, 3, 4, 5, 6]; let (even, odd) = partition(&mut even_odd, |x| x & 1 == 0); assert!(&[0, 2, 4, 6].iter().all(|x| even.iter().any(|e| e == x)), "expected [0, 2, 4, 6], got {:?}", even); assert!(&[1, 3, 5].iter().all(|x| odd.iter().any(|o| o == x)));