Primitive Type slice
[-]
[+]
Utilities for slice manipulation
The slice
module contains useful code to help work with slice values.
Slices are a view into a block of memory represented as a pointer and a length.
// slicing a Vec let vec = vec!(1i, 2, 3); let int_slice = vec.as_slice(); // coercing an array to a slice let str_slice: &[&str] = &["one", "two", "three"];
Slices are either mutable or shared. The shared slice type is &[T]
,
while the mutable slice type is &mut[T]
. For example, you can mutate the
block of memory that a mutable slice points to:
let x: &mut[int] = &mut [1i, 2, 3]; x[1] = 7; assert_eq!(x[0], 1); assert_eq!(x[1], 7); assert_eq!(x[2], 3);
Here are some of the things this module contains:
Structs
There are several structs that are useful for slices, such as Iter
, which
represents iteration over a slice.
Traits
A number of traits add methods that allow you to accomplish tasks
with slices, the most important being SliceExt
. Other traits
apply only to slices of elements satisfying certain bounds (like
Ord
).
An example is the slice
method which enables slicing syntax [a..b]
that
returns an immutable "view" into a Vec
or another slice from the index
interval [a, b)
:
#![feature(slicing_syntax)] fn main() { let numbers = [0i, 1i, 2i]; let last_numbers = numbers[1..3]; // last_numbers is now &[1i, 2i] }
Implementations of other traits
There are several implementations of common traits for slices. Some examples include:
Clone
Eq
,Ord
- for immutable slices whose element type areEq
orOrd
.Hash
- for slices whose element type isHash
Iteration
The method iter()
returns an iteration value for a slice. The iterator
yields references to the slice's elements, so if the element
type of the slice is int
, the element type of the iterator is &int
.
let numbers = [0i, 1i, 2i]; for &x in numbers.iter() { println!("{} is a number!", x); }
.iter_mut()
returns an iterator that allows modifying each value.- Further iterators exist that split, chunk or permute the slice.
Trait Implementations
impl<T> SliceExt<T> for [T]
fn sort_by<F>(&mut self, compare: F) where F: FnMut(&T, &T) -> Ordering
fn move_from(&mut self, src: Vec<T>, start: uint, end: uint) -> uint
fn slice<'a>(&'a self, start: uint, end: uint) -> &'a [T]
fn slice_from<'a>(&'a self, start: uint) -> &'a [T]
fn slice_to<'a>(&'a self, end: uint) -> &'a [T]
fn split_at<'a>(&'a self, mid: uint) -> (&'a [T], &'a [T])
fn iter<'a>(&'a self) -> Iter<'a, T>
fn split<F>(&self, pred: F) -> Split<T, F> where F: FnMut(&T) -> bool
fn splitn<F>(&self, n: uint, pred: F) -> SplitN<T, F> where F: FnMut(&T) -> bool
fn rsplitn<F>(&self, n: uint, pred: F) -> RSplitN<T, F> where F: FnMut(&T) -> bool
fn windows<'a>(&'a self, size: uint) -> Windows<'a, T>
fn chunks<'a>(&'a self, size: uint) -> Chunks<'a, T>
fn get<'a>(&'a self, index: uint) -> Option<&'a T>
fn first<'a>(&'a self) -> Option<&'a T>
fn tail<'a>(&'a self) -> &'a [T]
fn init<'a>(&'a self) -> &'a [T]
fn last<'a>(&'a self) -> Option<&'a T>
unsafe fn get_unchecked<'a>(&'a self, index: uint) -> &'a T
fn as_ptr(&self) -> *const T
fn binary_search_by<F>(&self, f: F) -> Result<uint, uint> where F: FnMut(&T) -> Ordering
fn len(&self) -> uint
fn is_empty(&self) -> bool
fn get_mut<'a>(&'a mut self, index: uint) -> Option<&'a mut T>
fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T]
fn slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T]
fn slice_from_mut<'a>(&'a mut self, start: uint) -> &'a mut [T]
fn slice_to_mut<'a>(&'a mut self, end: uint) -> &'a mut [T]
fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T>
fn first_mut<'a>(&'a mut self) -> Option<&'a mut T>
fn tail_mut<'a>(&'a mut self) -> &'a mut [T]
fn init_mut<'a>(&'a mut self) -> &'a mut [T]
fn last_mut<'a>(&'a mut self) -> Option<&'a mut T>
fn split_mut<F>(&mut self, pred: F) -> SplitMut<T, F> where F: FnMut(&T) -> bool
fn splitn_mut<F>(&mut self, n: uint, pred: F) -> SplitNMut<T, F> where F: FnMut(&T) -> bool
fn rsplitn_mut<F>(&mut self, n: uint, pred: F) -> RSplitNMut<T, F> where F: FnMut(&T) -> bool
fn chunks_mut<'a>(&'a mut self, chunk_size: uint) -> ChunksMut<'a, T>
fn swap(&mut self, a: uint, b: uint)
fn split_at_mut<'a>(&'a mut self, mid: uint) -> (&'a mut [T], &'a mut [T])
fn reverse(&mut self)
unsafe fn get_unchecked_mut<'a>(&'a mut self, index: uint) -> &'a mut T
fn as_mut_ptr(&mut self) -> *mut T
fn head(&self) -> Option<&T>
unsafe fn unsafe_get(&self, index: uint) -> &T
fn head_mut(&mut self) -> Option<&mut T>
unsafe fn unchecked_mut(&mut self, index: uint) -> &mut T
impl<T: Clone> CloneSliceExt<T> for [T]
fn to_vec(&self) -> Vec<T>
Returns a copy of v
.
fn partitioned<F>(&self, f: F) -> (Vec<T>, Vec<T>) where F: FnMut(&T) -> bool
fn permutations(&self) -> Permutations<T>
Returns an iterator over all permutations of a vector.