Trait std::slice::SliceExtStable [-]  [+] [src]

pub trait SliceExt {
    type Item;
    fn sort_by<F>(&mut self, compare: F);
    fn move_from(&mut self, src: Vec<<Self as SliceExt>::Item>, start: usize, end: usize) -> usize;
    fn slice(&self, start: usize, end: usize) -> &[<Self as SliceExt>::Item];
    fn slice_from(&self, start: usize) -> &[<Self as SliceExt>::Item];
    fn slice_to(&self, end: usize) -> &[<Self as SliceExt>::Item];
    fn split_at(&self, mid: usize) -> (&[<Self as SliceExt>::Item], &[<Self as SliceExt>::Item]);
    fn iter(&self) -> Iter<<Self as SliceExt>::Item>;
    fn split<F>(&self, pred: F) -> Split<<Self as SliceExt>::Item, F>;
    fn splitn<F>(&self, n: usize, pred: F) -> SplitN<<Self as SliceExt>::Item, F>;
    fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<<Self as SliceExt>::Item, F>;
    fn windows(&self, size: usize) -> Windows<<Self as SliceExt>::Item>;
    fn chunks(&self, size: usize) -> Chunks<<Self as SliceExt>::Item>;
    fn get(&self, index: usize) -> Option<&<Self as SliceExt>::Item>;
    fn first(&self) -> Option<&<Self as SliceExt>::Item>;
    fn tail(&self) -> &[<Self as SliceExt>::Item];
    fn init(&self) -> &[<Self as SliceExt>::Item];
    fn last(&self) -> Option<&<Self as SliceExt>::Item>;
    unsafe fn get_unchecked(&self, index: usize) -> &<Self as SliceExt>::Item;
    fn as_ptr(&self) -> *const <Self as SliceExt>::Item;
    fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>;
    fn len(&self) -> usize;
    fn get_mut(&mut self, index: usize) -> Option<&mut <Self as SliceExt>::Item>;
    fn as_mut_slice(&mut self) -> &mut [<Self as SliceExt>::Item];
    fn slice_mut(&mut self, start: usize, end: usize) -> &mut [<Self as SliceExt>::Item];
    fn slice_from_mut(&mut self, start: usize) -> &mut [<Self as SliceExt>::Item];
    fn slice_to_mut(&mut self, end: usize) -> &mut [<Self as SliceExt>::Item];
    fn iter_mut(&mut self) -> IterMut<<Self as SliceExt>::Item>;
    fn first_mut(&mut self) -> Option<&mut <Self as SliceExt>::Item>;
    fn tail_mut(&mut self) -> &mut [<Self as SliceExt>::Item];
    fn init_mut(&mut self) -> &mut [<Self as SliceExt>::Item];
    fn last_mut(&mut self) -> Option<&mut <Self as SliceExt>::Item>;
    fn split_mut<F>(&mut self, pred: F) -> SplitMut<<Self as SliceExt>::Item, F>;
    fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<<Self as SliceExt>::Item, F>;
    fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<<Self as SliceExt>::Item, F>;
    fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<<Self as SliceExt>::Item>;
    fn swap(&mut self, a: usize, b: usize);
    fn split_at_mut(&mut self, mid: usize) -> (&mut [<Self as SliceExt>::Item], &mut [<Self as SliceExt>::Item]);
    fn reverse(&mut self);
    unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut <Self as SliceExt>::Item;
    fn as_mut_ptr(&mut self) -> *mut <Self as SliceExt>::Item;
    fn to_vec(&self) -> Vec<<Self as SliceExt>::Item>;
    fn permutations(&self) -> Permutations<<Self as SliceExt>::Item>;
    fn clone_from_slice(&mut self, &[<Self as SliceExt>::Item]) -> usize;
    fn sort(&mut self);
    fn binary_search(&self, x: &<Self as SliceExt>::Item) -> Result<usize, usize>;
    fn next_permutation(&mut self) -> bool;
    fn prev_permutation(&mut self) -> bool;
    fn position_elem(&self, t: &<Self as SliceExt>::Item) -> Option<usize>;
    fn rposition_elem(&self, t: &<Self as SliceExt>::Item) -> Option<usize>;
    fn contains(&self, x: &<Self as SliceExt>::Item) -> bool;
    fn starts_with(&self, needle: &[<Self as SliceExt>::Item]) -> bool;
    fn ends_with(&self, needle: &[<Self as SliceExt>::Item]) -> bool;
    fn into_vec(self: Box<Self>) -> Vec<<Self as SliceExt>::Item>;

    fn is_empty(&self) -> bool { ... }
    fn binary_search_elem(&self, x: &<Self as SliceExt>::Item) -> Result<usize, usize> { ... }
}

Allocating extension methods for slices.

Required Methods

type Item

fn sort_by<F>(&mut self, compare: F)

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<<Self as SliceExt>::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 src to start copying from
  • end - The index into src to 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) -> &[<Self as SliceExt>::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) -> &[<Self as SliceExt>::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) -> &[<Self as SliceExt>::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) -> (&[<Self as SliceExt>::Item], &[<Self as SliceExt>::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<<Self as SliceExt>::Item>

Returns an iterator over the slice

fn split<F>(&self, pred: F) -> Split<<Self as SliceExt>::Item, F>

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<<Self as SliceExt>::Item, F>

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<<Self as SliceExt>::Item, F>

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<<Self as SliceExt>::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]):

fn main() { let v = &[1i, 2, 3, 4]; for win in v.windows(2) { println!("{:?}", win); } }
let v = &[1i, 2, 3, 4];
for win in v.windows(2) {
    println!("{:?}", win);
}

fn chunks(&self, size: usize) -> Chunks<<Self as SliceExt>::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]):

fn main() { let v = &[1i, 2, 3, 4, 5]; for win in v.chunks(2) { println!("{:?}", win); } }
let v = &[1i, 2, 3, 4, 5];
for win in v.chunks(2) {
    println!("{:?}", win);
}

fn get(&self, index: usize) -> Option<&<Self as SliceExt>::Item>

Returns the element of a slice at the given index, or None if the index is out of bounds.

fn first(&self) -> Option<&<Self as SliceExt>::Item>

Returns the first element of a slice, or None if it is empty.

fn tail(&self) -> &[<Self as SliceExt>::Item]

Returns all but the first element of a slice.

fn init(&self) -> &[<Self as SliceExt>::Item]

Returns all but the last element of a slice.

fn last(&self) -> Option<&<Self as SliceExt>::Item>

Returns the last element of a slice, or None if it is empty.

unsafe fn get_unchecked(&self, index: usize) -> &<Self as SliceExt>::Item

Returns a pointer to the element at the given index, without doing bounds checking.

fn as_ptr(&self) -> *const <Self as SliceExt>::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>

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].

fn main() { 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, }); }
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 <Self as SliceExt>::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 [<Self as SliceExt>::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 [<Self as SliceExt>::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 [<Self as SliceExt>::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 [<Self as SliceExt>::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<<Self as SliceExt>::Item>

Returns an iterator that allows modifying each value

fn first_mut(&mut self) -> Option<&mut <Self as SliceExt>::Item>

Returns a mutable pointer to the first element of a slice, or None if it is empty

fn tail_mut(&mut self) -> &mut [<Self as SliceExt>::Item]

Returns all but the first element of a mutable slice

fn init_mut(&mut self) -> &mut [<Self as SliceExt>::Item]

Returns all but the last element of a mutable slice

fn last_mut(&mut self) -> Option<&mut <Self as SliceExt>::Item>

Returns a mutable pointer to the last item in the slice.

fn split_mut<F>(&mut self, pred: F) -> SplitMut<<Self as SliceExt>::Item, F>

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<<Self as SliceExt>::Item, F>

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<<Self as SliceExt>::Item, F>

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<<Self as SliceExt>::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 [<Self as SliceExt>::Item], &mut [<Self as SliceExt>::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 <Self as SliceExt>::Item

Returns an unsafe mutable pointer to the element in index

fn as_mut_ptr(&mut self) -> *mut <Self as SliceExt>::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<<Self as SliceExt>::Item>

Copies self into a new Vec.

fn permutations(&self) -> Permutations<<Self as SliceExt>::Item>

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, &[<Self as SliceExt>::Item]) -> usize

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)

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]);

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].

fn main() { 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, }); }
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

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

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: &<Self as SliceExt>::Item) -> Option<usize>

Find the first index containing a matching value.

fn rposition_elem(&self, t: &<Self as SliceExt>::Item) -> Option<usize>

Find the last index containing a matching value.

fn contains(&self, x: &<Self as SliceExt>::Item) -> bool

Return true if the slice contains an element with the given value.

fn starts_with(&self, needle: &[<Self as SliceExt>::Item]) -> bool

Returns true if needle is a prefix of the slice.

fn ends_with(&self, needle: &[<Self as SliceExt>::Item]) -> bool

Returns true if needle is a suffix of the slice.

fn into_vec(self: Box<Self>) -> Vec<<Self as SliceExt>::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: &<Self as SliceExt>::Item) -> Result<usize, usize>

Deprecated: use binary_search instead.

Implementors