Trait collections::slice::SliceExtStable
[-]
[+]
[src]
pub trait SliceExt {
type Item;
fn sort_by<F>(&mut self, compare: F) where F: FnMut(&Item, &Item) -> Ordering;
fn move_from(&mut self, src: Vec<Item>, start: usize, end: usize) -> usize;
fn slice(&self, start: usize, end: usize) -> &[Item];
fn slice_from(&self, start: usize) -> &[Item];
fn slice_to(&self, end: usize) -> &[Item];
fn split_at(&self, mid: usize) -> (&[Item], &[Item]);
fn iter(&self) -> Iter<Item>;
fn split<F>(&self, pred: F) -> Split<Item, F> where F: FnMut(&Item) -> bool;
fn splitn<F>(&self, n: usize, pred: F) -> SplitN<Item, F> where F: FnMut(&Item) -> bool;
fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<Item, F> where F: FnMut(&Item) -> bool;
fn windows(&self, size: usize) -> Windows<Item>;
fn chunks(&self, size: usize) -> Chunks<Item>;
fn get(&self, index: usize) -> Option<&Item>;
fn first(&self) -> Option<&Item>;
fn tail(&self) -> &[Item];
fn init(&self) -> &[Item];
fn last(&self) -> Option<&Item>;
unsafe fn get_unchecked(&self, index: usize) -> &Item;
fn as_ptr(&self) -> *const Item;
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize> where F: FnMut(&Item) -> Ordering;
fn len(&self) -> usize;
fn get_mut(&mut self, index: usize) -> Option<&mut Item>;
fn as_mut_slice(&mut self) -> &mut [Item];
fn slice_mut(&mut self, start: usize, end: usize) -> &mut [Item];
fn slice_from_mut(&mut self, start: usize) -> &mut [Item];
fn slice_to_mut(&mut self, end: usize) -> &mut [Item];
fn iter_mut(&mut self) -> IterMut<Item>;
fn first_mut(&mut self) -> Option<&mut Item>;
fn tail_mut(&mut self) -> &mut [Item];
fn init_mut(&mut self) -> &mut [Item];
fn last_mut(&mut self) -> Option<&mut Item>;
fn split_mut<F>(&mut self, pred: F) -> SplitMut<Item, F> where F: FnMut(&Item) -> bool;
fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<Item, F> where F: FnMut(&Item) -> bool;
fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<Item, F> where F: FnMut(&Item) -> bool;
fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<Item>;
fn swap(&mut self, a: usize, b: usize);
fn split_at_mut(&mut self, mid: usize) -> (&mut [Item], &mut [Item]);
fn reverse(&mut self);
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Item;
fn as_mut_ptr(&mut self) -> *mut Item;
fn to_vec(&self) -> Vec<Item> where Item: Clone;
fn permutations(&self) -> Permutations<Item> where Item: Clone;
fn clone_from_slice(&mut self, &[Item]) -> usize where Item: Clone;
fn sort(&mut self) where Item: Ord;
fn binary_search(&self, x: &Item) -> Result<usize, usize> where Item: Ord;
fn next_permutation(&mut self) -> bool where Item: Ord;
fn prev_permutation(&mut self) -> bool where Item: Ord;
fn position_elem(&self, t: &Item) -> Option<usize> where Item: PartialEq;
fn rposition_elem(&self, t: &Item) -> Option<usize> where Item: PartialEq;
fn contains(&self, x: &Item) -> bool where Item: PartialEq;
fn starts_with(&self, needle: &[Item]) -> bool where Item: PartialEq;
fn ends_with(&self, needle: &[Item]) -> bool where Item: PartialEq;
fn into_vec(self: Box<Self>) -> Vec<Item>;
fn is_empty(&self) -> bool { ... }
fn binary_search_elem(&self, x: &Item) -> Result<usize, usize> where Item: Ord { ... }
}Allocating extension methods for slices.
Associated Types
Required Methods
fn sort_by<F>(&mut self, compare: F) where F: FnMut(&Item, &Item) -> Ordering
Sorts the slice, in place, using compare to compare
elements.
This sort is O(n log n) worst-case and stable, but allocates
approximately 2 * n, where n is the length of self.
Examples
fn main() { let mut v = [5i, 4, 1, 3, 2]; v.sort_by(|a, b| a.cmp(b)); assert!(v == [1, 2, 3, 4, 5]); // reverse sorting v.sort_by(|a, b| b.cmp(a)); assert!(v == [5, 4, 3, 2, 1]); }let mut v = [5i, 4, 1, 3, 2]; v.sort_by(|a, b| a.cmp(b)); assert!(v == [1, 2, 3, 4, 5]); // reverse sorting v.sort_by(|a, b| b.cmp(a)); assert!(v == [5, 4, 3, 2, 1]);
fn move_from(&mut self, src: Vec<Item>, start: usize, end: usize) -> usize
Consumes src and moves as many elements as it can into self
from the range [start,end).
Returns the number of elements copied (the shorter of self.len()
and end - start).
Arguments
- src - A mutable vector of
T - start - The index into
srcto start copying from - end - The index into
srcto stop copying from
Examples
fn main() { let mut a = [1i, 2, 3, 4, 5]; let b = vec![6i, 7, 8]; let num_moved = a.move_from(b, 0, 3); assert_eq!(num_moved, 3); assert!(a == [6i, 7, 8, 4, 5]); }let mut a = [1i, 2, 3, 4, 5]; let b = vec![6i, 7, 8]; let num_moved = a.move_from(b, 0, 3); assert_eq!(num_moved, 3); assert!(a == [6i, 7, 8, 4, 5]);
fn slice(&self, start: usize, end: usize) -> &[Item]
Returns a subslice spanning the interval [start, end).
Panics when the end of the new slice lies beyond the end of the
original slice (i.e. when end > self.len()) or when start > end.
Slicing with start equal to end yields an empty slice.
fn slice_from(&self, start: usize) -> &[Item]
Returns a subslice from start to the end of the slice.
Panics when start is strictly greater than the length of the original slice.
Slicing from self.len() yields an empty slice.
fn slice_to(&self, end: usize) -> &[Item]
Returns a subslice from the start of the slice to end.
Panics when end is strictly greater than the length of the original slice.
Slicing to 0 yields an empty slice.
fn split_at(&self, mid: usize) -> (&[Item], &[Item])
Divides one slice into two at an index.
The first will contain all indices from [0, mid) (excluding
the index mid itself) and the second will contain all
indices from [mid, len) (excluding the index len itself).
Panics if mid > len.
fn iter(&self) -> Iter<Item>
Returns an iterator over the slice
fn split<F>(&self, pred: F) -> Split<Item, F> where F: FnMut(&Item) -> bool
Returns an iterator over subslices separated by elements that match
pred. The matched element is not contained in the subslices.
fn splitn<F>(&self, n: usize, pred: F) -> SplitN<Item, F> where F: FnMut(&Item) -> bool
Returns an iterator over subslices separated by elements that match
pred, limited to splitting at most n times. The matched element is
not contained in the subslices.
fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<Item, F> where F: FnMut(&Item) -> bool
Returns an iterator over subslices separated by elements that match
pred limited to splitting at most n times. This starts at the end of
the slice and works backwards. The matched element is not contained in
the subslices.
fn windows(&self, size: usize) -> Windows<Item>
Returns an iterator over all contiguous windows of length
size. The windows overlap. If the slice is shorter than
size, the iterator returns no values.
Panics
Panics if size is 0.
Example
Print the adjacent pairs of a slice (i.e. [1,2], [2,3],
[3,4]):
let v = &[1i, 2, 3, 4]; for win in v.windows(2) { println!("{:?}", win); }
fn chunks(&self, size: usize) -> Chunks<Item>
Returns an iterator over size elements of the slice at a
time. The chunks do not overlap. If size does not divide the
length of the slice, then the last chunk will not have length
size.
Panics
Panics if size is 0.
Example
Print the slice two elements at a time (i.e. [1,2],
[3,4], [5]):
let v = &[1i, 2, 3, 4, 5]; for win in v.chunks(2) { println!("{:?}", win); }
fn get(&self, index: usize) -> Option<&Item>
Returns the element of a slice at the given index, or None if the
index is out of bounds.
fn first(&self) -> Option<&Item>
Returns the first element of a slice, or None if it is empty.
fn tail(&self) -> &[Item]
Returns all but the first element of a slice.
fn init(&self) -> &[Item]
Returns all but the last element of a slice.
fn last(&self) -> Option<&Item>
Returns the last element of a slice, or None if it is empty.
unsafe fn get_unchecked(&self, index: usize) -> &Item
Returns a pointer to the element at the given index, without doing bounds checking.
fn as_ptr(&self) -> *const Item
Returns an unsafe pointer to the slice's buffer
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
Modifying the slice may cause its buffer to be reallocated, which would also make any pointers to it invalid.
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize> where F: FnMut(&Item) -> Ordering
Binary search a sorted slice with a comparator function.
The comparator function should implement an order consistent
with the sort order of the underlying slice, returning an
order code that indicates whether its argument is Less,
Equal or Greater the desired target.
If a matching value is found then returns Ok, containing
the index for the matched element; if no match is found then
Err is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
Example
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in [1,4].
let s = [0i, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let s = s.as_slice(); let seek = 13; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9)); let seek = 4; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7)); let seek = 100; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13)); let seek = 1; let r = s.binary_search_by(|probe| probe.cmp(&seek)); assert!(match r { Ok(1...4) => true, _ => false, });
fn len(&self) -> usize
Return the number of elements in the slice
Example
fn main() { let a = [1i, 2, 3]; assert_eq!(a.len(), 3); }let a = [1i, 2, 3]; assert_eq!(a.len(), 3);
fn get_mut(&mut self, index: usize) -> Option<&mut Item>
Returns a mutable reference to the element at the given index,
or None if the index is out of bounds
fn as_mut_slice(&mut self) -> &mut [Item]
Work with self as a mut slice.
Primarily intended for getting a &mut [T] from a [T; N].
fn slice_mut(&mut self, start: usize, end: usize) -> &mut [Item]
Returns a mutable subslice spanning the interval [start, end).
Panics when the end of the new slice lies beyond the end of the
original slice (i.e. when end > self.len()) or when start > end.
Slicing with start equal to end yields an empty slice.
fn slice_from_mut(&mut self, start: usize) -> &mut [Item]
Returns a mutable subslice from start to the end of the slice.
Panics when start is strictly greater than the length of the original slice.
Slicing from self.len() yields an empty slice.
fn slice_to_mut(&mut self, end: usize) -> &mut [Item]
Returns a mutable subslice from the start of the slice to end.
Panics when end is strictly greater than the length of the original slice.
Slicing to 0 yields an empty slice.
fn iter_mut(&mut self) -> IterMut<Item>
Returns an iterator that allows modifying each value
fn first_mut(&mut self) -> Option<&mut Item>
Returns a mutable pointer to the first element of a slice, or None if it is empty
fn tail_mut(&mut self) -> &mut [Item]
Returns all but the first element of a mutable slice
fn init_mut(&mut self) -> &mut [Item]
Returns all but the last element of a mutable slice
fn last_mut(&mut self) -> Option<&mut Item>
Returns a mutable pointer to the last item in the slice.
fn split_mut<F>(&mut self, pred: F) -> SplitMut<Item, F> where F: FnMut(&Item) -> bool
Returns an iterator over mutable subslices separated by elements that
match pred. The matched element is not contained in the subslices.
fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<Item, F> where F: FnMut(&Item) -> bool
Returns an iterator over subslices separated by elements that match
pred, limited to splitting at most n times. The matched element is
not contained in the subslices.
fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<Item, F> where F: FnMut(&Item) -> bool
Returns an iterator over subslices separated by elements that match
pred limited to splitting at most n times. This starts at the end of
the slice and works backwards. The matched element is not contained in
the subslices.
fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<Item>
Returns an iterator over chunk_size elements of the slice at a time.
The chunks are mutable and do not overlap. If chunk_size does
not divide the length of the slice, then the last chunk will not
have length chunk_size.
Panics
Panics if chunk_size is 0.
fn swap(&mut self, a: usize, b: usize)
Swaps two elements in a slice.
Arguments
- a - The index of the first element
- b - The index of the second element
Panics
Panics if a or b are out of bounds.
Example
fn main() { let mut v = ["a", "b", "c", "d"]; v.swap(1, 3); assert!(v == ["a", "d", "c", "b"]); }let mut v = ["a", "b", "c", "d"]; v.swap(1, 3); assert!(v == ["a", "d", "c", "b"]);
fn split_at_mut(&mut self, mid: usize) -> (&mut [Item], &mut [Item])
Divides one &mut into two at an index.
The first will contain all indices from [0, mid) (excluding
the index mid itself) and the second will contain all
indices from [mid, len) (excluding the index len itself).
Panics
Panics if mid > len.
Example
fn main() { let mut v = [1i, 2, 3, 4, 5, 6]; // scoped to restrict the lifetime of the borrows { let (left, right) = v.split_at_mut(0); assert!(left == []); assert!(right == [1i, 2, 3, 4, 5, 6]); } { let (left, right) = v.split_at_mut(2); assert!(left == [1i, 2]); assert!(right == [3i, 4, 5, 6]); } { let (left, right) = v.split_at_mut(6); assert!(left == [1i, 2, 3, 4, 5, 6]); assert!(right == []); } }let mut v = [1i, 2, 3, 4, 5, 6]; // scoped to restrict the lifetime of the borrows { let (left, right) = v.split_at_mut(0); assert!(left == []); assert!(right == [1i, 2, 3, 4, 5, 6]); } { let (left, right) = v.split_at_mut(2); assert!(left == [1i, 2]); assert!(right == [3i, 4, 5, 6]); } { let (left, right) = v.split_at_mut(6); assert!(left == [1i, 2, 3, 4, 5, 6]); assert!(right == []); }
fn reverse(&mut self)
Reverse the order of elements in a slice, in place.
Example
fn main() { let mut v = [1i, 2, 3]; v.reverse(); assert!(v == [3i, 2, 1]); }let mut v = [1i, 2, 3]; v.reverse(); assert!(v == [3i, 2, 1]);
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Item
Returns an unsafe mutable pointer to the element in index
fn as_mut_ptr(&mut self) -> *mut Item
Return an unsafe mutable pointer to the slice's buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
Modifying the slice may cause its buffer to be reallocated, which would also make any pointers to it invalid.
fn to_vec(&self) -> Vec<Item> where Item: Clone
Copies self into a new Vec.
fn permutations(&self) -> Permutations<Item> where Item: Clone
Creates an iterator that yields every possible permutation of the vector in succession.
Examples
fn main() { let v = [1i, 2, 3]; let mut perms = v.permutations(); for p in perms { println!("{:?}", p); } }let v = [1i, 2, 3]; let mut perms = v.permutations(); for p in perms { println!("{:?}", p); }
Iterating through permutations one by one.
fn main() { let v = [1i, 2, 3]; let mut perms = v.permutations(); assert_eq!(Some(vec![1i, 2, 3]), perms.next()); assert_eq!(Some(vec![1i, 3, 2]), perms.next()); assert_eq!(Some(vec![3i, 1, 2]), perms.next()); }let v = [1i, 2, 3]; let mut perms = v.permutations(); assert_eq!(Some(vec![1i, 2, 3]), perms.next()); assert_eq!(Some(vec![1i, 3, 2]), perms.next()); assert_eq!(Some(vec![3i, 1, 2]), perms.next());
fn clone_from_slice(&mut self, &[Item]) -> usize where Item: Clone
Copies as many elements from src as it can into self (the
shorter of self.len() and src.len()). Returns the number
of elements copied.
Example
fn main() { let mut dst = [0i, 0, 0]; let src = [1i, 2]; assert!(dst.clone_from_slice(&src) == 2); assert!(dst == [1, 2, 0]); let src2 = [3i, 4, 5, 6]; assert!(dst.clone_from_slice(&src2) == 3); assert!(dst == [3i, 4, 5]); }let mut dst = [0i, 0, 0]; let src = [1i, 2]; assert!(dst.clone_from_slice(&src) == 2); assert!(dst == [1, 2, 0]); let src2 = [3i, 4, 5, 6]; assert!(dst.clone_from_slice(&src2) == 3); assert!(dst == [3i, 4, 5]);
fn sort(&mut self) where Item: Ord
Sorts the slice, in place.
This is equivalent to self.sort_by(|a, b| a.cmp(b)).
Examples
fn main() { let mut v = [-5i, 4, 1, -3, 2]; v.sort(); assert!(v == [-5i, -3, 1, 2, 4]); }let mut v = [-5i, 4, 1, -3, 2]; v.sort(); assert!(v == [-5i, -3, 1, 2, 4]);
fn binary_search(&self, x: &Item) -> Result<usize, usize> where Item: Ord
Binary search a sorted slice for a given element.
If the value is found then Ok is returned, containing the
index of the matching element; if the value is not found then
Err is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
Example
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in [1,4].
let s = [0i, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let s = s.as_slice(); assert_eq!(s.binary_search(&13), Ok(9)); assert_eq!(s.binary_search(&4), Err(7)); assert_eq!(s.binary_search(&100), Err(13)); let r = s.binary_search(&1); assert!(match r { Ok(1...4) => true, _ => false, });
fn next_permutation(&mut self) -> bool where Item: Ord
Mutates the slice to the next lexicographic permutation.
Returns true if successful and false if the slice is at the
last-ordered permutation.
Example
fn main() { let v: &mut [_] = &mut [0i, 1, 2]; v.next_permutation(); let b: &mut [_] = &mut [0i, 2, 1]; assert!(v == b); v.next_permutation(); let b: &mut [_] = &mut [1i, 0, 2]; assert!(v == b); }let v: &mut [_] = &mut [0i, 1, 2]; v.next_permutation(); let b: &mut [_] = &mut [0i, 2, 1]; assert!(v == b); v.next_permutation(); let b: &mut [_] = &mut [1i, 0, 2]; assert!(v == b);
fn prev_permutation(&mut self) -> bool where Item: Ord
Mutates the slice to the previous lexicographic permutation.
Returns true if successful and false if the slice is at the
first-ordered permutation.
Example
fn main() { let v: &mut [_] = &mut [1i, 0, 2]; v.prev_permutation(); let b: &mut [_] = &mut [0i, 2, 1]; assert!(v == b); v.prev_permutation(); let b: &mut [_] = &mut [0i, 1, 2]; assert!(v == b); }let v: &mut [_] = &mut [1i, 0, 2]; v.prev_permutation(); let b: &mut [_] = &mut [0i, 2, 1]; assert!(v == b); v.prev_permutation(); let b: &mut [_] = &mut [0i, 1, 2]; assert!(v == b);
fn position_elem(&self, t: &Item) -> Option<usize> where Item: PartialEq
Find the first index containing a matching value.
fn rposition_elem(&self, t: &Item) -> Option<usize> where Item: PartialEq
Find the last index containing a matching value.
fn contains(&self, x: &Item) -> bool where Item: PartialEq
Return true if the slice contains an element with the given value.
fn starts_with(&self, needle: &[Item]) -> bool where Item: PartialEq
Returns true if needle is a prefix of the slice.
fn ends_with(&self, needle: &[Item]) -> bool where Item: PartialEq
Returns true if needle is a suffix of the slice.
fn into_vec(self: Box<Self>) -> Vec<Item>
Convert self into a vector without clones or allocation.
Provided Methods
fn is_empty(&self) -> bool
Returns true if the slice has a length of 0
Example
fn main() { let a = [1i, 2, 3]; assert!(!a.is_empty()); }let a = [1i, 2, 3]; assert!(!a.is_empty());
fn binary_search_elem(&self, x: &Item) -> Result<usize, usize> where Item: Ord
Deprecated: use binary_search instead.