Primitive Type slice

Utilities for vector manipulation

The vec module contains useful code to help work with vector values. Vectors are Rust's list type. Vectors contain zero or more values of homogeneous types:

fn main() { let int_vector = [1,2,3]; let str_vector = ["one", "two", "three"]; }
let int_vector = [1,2,3];
let str_vector = ["one", "two", "three"];

This is a big module, but for a high-level overview:

Structs

Several structs that are useful for vectors, such as Items, which represents iteration over a vector.

Traits

A number of traits add methods that allow you to accomplish tasks with vectors.

Traits defined for the &[T] type (a vector slice), have methods that can be called on either owned vectors, denoted ~[T], or on vector slices themselves. These traits include ImmutableVector, and MutableVector for the &mut [T] case.

An example is the method .slice(a, b) that returns an immutable "view" into a vector or a vector slice from the index interval [a, b):

fn main() { let numbers = [0, 1, 2]; let last_numbers = numbers.slice(1, 3); // last_numbers is now &[1, 2] }
let numbers = [0, 1, 2];
let last_numbers = numbers.slice(1, 3);
// last_numbers is now &[1, 2]

Traits defined for the ~[T] type, like OwnedVector, can only be called on such vectors. These methods deal with adding elements or otherwise changing the allocation of the vector.

An example is the method .push(element) that will add an element at the end of the vector:

fn main() { let mut numbers = vec![0, 1, 2]; numbers.push(7); // numbers is now vec![0, 1, 2, 7]; }
let mut numbers = vec![0, 1, 2];
numbers.push(7);
// numbers is now vec![0, 1, 2, 7];

Implementations of other traits

Vectors are a very useful type, and so there's several implementations of traits from other modules. Some notable examples:

Iteration

The method iter() returns an iteration value for a vector or a vector slice. The iterator yields references to the vector's elements, so if the element type of the vector is int, the element type of the iterator is &int.

fn main() { let numbers = [0i, 1i, 2i]; for &x in numbers.iter() { println!("{} is a number!", x); } }
let numbers = [0i, 1i, 2i];
for &x in numbers.iter() {
    println!("{} is a number!", x);
}

Function definitions

There are a number of free functions that create or take vectors, for example:

Trait Implementations

impl<'a> AsciiCast<&'a [Ascii]> for &'a [u8]

unsafe fn to_ascii_nocheck(&self) -> &'a [Ascii]

fn is_ascii(&self) -> bool

fn to_ascii(&self) -> T

fn to_ascii_opt(&self) -> Option<T>

impl<'a> AsciiStr for &'a [Ascii]

fn as_str_ascii<'a>(&'a self) -> &'a str

fn to_lower(&self) -> Vec<Ascii>

fn to_upper(&self) -> Vec<Ascii>

fn eq_ignore_case(self, other: &[Ascii]) -> bool

impl<'a, T> Clone for &'a [T]

fn clone<'a, T>(&self) -> &'a [T]

Return a shallow copy of the slice.

fn clone_from<'a, T>(&mut self, &&'a [T])

impl<'a, T> Repr<Slice<T>> for &'a [T]

fn repr<'a, T>(&self) -> Slice<T>

impl<'a, T: PartialEq> PartialEq for &'a [T]

fn eq<'a, T: PartialEq>(&self, other: &&'a [T]) -> bool

fn ne<'a, T: PartialEq>(&self, other: &&'a [T]) -> bool

fn ne<'a, T: PartialEq>(&self, &&'a [T]) -> bool

impl<'a, T: Eq> Eq for &'a [T]

fn assert_receiver_is_total_eq<'a, T: Eq>(&self)

impl<'a, T: PartialEq, V: Vector<T>> Equiv<V> for &'a [T]

fn equiv<'a, T: PartialEq, V: Vector<T>>(&self, other: &V) -> bool

impl<'a, T: Ord> Ord for &'a [T]

fn cmp<'a, T: Ord>(&self, other: &&'a [T]) -> Ordering

impl<'a, T: PartialOrd> PartialOrd for &'a [T]

fn lt<'a, T: PartialOrd>(&self, other: &&'a [T]) -> bool

fn le<'a, T: PartialOrd>(&self, other: &&'a [T]) -> bool

fn ge<'a, T: PartialOrd>(&self, other: &&'a [T]) -> bool

fn gt<'a, T: PartialOrd>(&self, other: &&'a [T]) -> bool

fn le<'a, T: PartialOrd>(&self, &&'a [T]) -> bool

fn gt<'a, T: PartialOrd>(&self, &&'a [T]) -> bool

fn ge<'a, T: PartialOrd>(&self, &&'a [T]) -> bool

impl<'a, T> Vector<T> for &'a [T]

fn as_slice<'a, T>(&'a self) -> &'a [T]

impl<'a, T> Collection for &'a [T]

fn len<'a, T>(&self) -> uint

Returns the length of a vector

fn is_empty<'a, T>(&self) -> bool

impl<'a, T> ImmutableVector<'a, T> for &'a [T]

fn slice<'a, T>(&self, start: uint, end: uint) -> &'a [T]

fn slice_from<'a, T>(&self, start: uint) -> &'a [T]

fn slice_to<'a, T>(&self, end: uint) -> &'a [T]

fn iter<'a, T>(self) -> Items<'a, T>

fn split<'a, T>(self, pred: |&T|: 'a -> bool) -> Splits<'a, T>

fn splitn<'a, T>(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T>

fn rsplitn<'a, T>(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T>

fn windows<'a, T>(self, size: uint) -> Windows<'a, T>

fn chunks<'a, T>(self, size: uint) -> Chunks<'a, T>

fn get<'a, T>(&self, index: uint) -> Option<&'a T>

fn head<'a, T>(&self) -> Option<&'a T>

fn tail<'a, T>(&self) -> &'a [T]

fn tailn<'a, T>(&self, n: uint) -> &'a [T]

fn init<'a, T>(&self) -> &'a [T]

fn initn<'a, T>(&self, n: uint) -> &'a [T]

fn last<'a, T>(&self) -> Option<&'a T>

unsafe fn unsafe_ref<'a, T>(self, index: uint) -> &'a T

fn as_ptr<'a, T>(&self) -> *T

fn bsearch<'a, T>(&self, f: |&T| -> Ordering) -> Option<uint>

fn shift_ref<'a, T>(&mut self) -> Option<&'a T>

fn pop_ref<'a, T>(&mut self) -> Option<&'a T>

impl<'a, T: PartialEq> ImmutableEqVector<T> for &'a [T]

fn position_elem<'a, T: PartialEq>(&self, x: &T) -> Option<uint>

fn rposition_elem<'a, T: PartialEq>(&self, t: &T) -> Option<uint>

fn contains<'a, T: PartialEq>(&self, x: &T) -> bool

fn starts_with<'a, T: PartialEq>(&self, needle: &[T]) -> bool

fn ends_with<'a, T: PartialEq>(&self, needle: &[T]) -> bool

impl<'a, T: Ord> ImmutableOrdVector<T> for &'a [T]

fn bsearch_elem<'a, T: Ord>(&self, x: &T) -> Option<uint>

impl<'a, T> MutableVector<'a, T> for &'a mut [T]

fn get_mut<'a, T>(self, index: uint) -> Option<&'a mut T>

fn as_mut_slice<'a, T>(self) -> &'a mut [T]

fn mut_slice<'a, T>(self, start: uint, end: uint) -> &'a mut [T]

fn mut_slice_from<'a, T>(self, start: uint) -> &'a mut [T]

fn mut_slice_to<'a, T>(self, end: uint) -> &'a mut [T]

fn mut_split_at<'a, T>(self, mid: uint) -> (&'a mut [T], &'a mut [T])

fn mut_iter<'a, T>(self) -> MutItems<'a, T>

fn mut_last<'a, T>(self) -> Option<&'a mut T>

fn mut_split<'a, T>(self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T>

fn mut_chunks<'a, T>(self, chunk_size: uint) -> MutChunks<'a, T>

fn mut_shift_ref<'a, T>(&mut self) -> Option<&'a mut T>

fn mut_pop_ref<'a, T>(&mut self) -> Option<&'a mut T>

fn swap<'a, T>(self, a: uint, b: uint)

fn reverse<'a, T>(self)

unsafe fn unsafe_mut_ref<'a, T>(self, index: uint) -> &'a mut T

fn as_mut_ptr<'a, T>(self) -> *mut T

unsafe fn unsafe_set<'a, T>(self, index: uint, val: T)

unsafe fn init_elem<'a, T>(self, i: uint, val: T)

unsafe fn copy_memory<'a, T>(self, src: &[T])

impl<'a, T: Clone> MutableCloneableVector<T> for &'a mut [T]

fn copy_from<'a, T: Clone>(self, src: &[T]) -> uint

impl<'a> MutableByteVector for &'a mut [u8]

fn set_memory<'a>(self, value: u8)

impl<'a, T> Default for &'a [T]

fn default<'a, T>() -> &'a [T]

impl<'a> CharEq for &'a [char]

fn matches<'a>(&mut self, c: char) -> bool

fn only_ascii<'a>(&self) -> bool

impl<'a, T: Show> Show for &'a [T]

fn fmt<'a, T: Show>(&self, f: &mut Formatter) -> Result<(), FormatError>

impl<'a, T: Show> Show for &'a mut [T]

fn fmt<'a, T: Show>(&self, f: &mut Formatter) -> Result<(), FormatError>

impl<'a, T: Clone, V: Vector<T>> VectorVector<T> for &'a [V]

fn concat_vec<'a, T: Clone, V: Vector<T>>(&self) -> Vec<T>

fn connect_vec<'a, T: Clone, V: Vector<T>>(&self, sep: &T) -> Vec<T>

impl<'a, T: Clone> CloneableVector<T> for &'a [T]

Extension methods for vector slices

fn to_owned<'a, T: Clone>(&self) -> Vec<T>

Returns a copy of v.

fn into_owned<'a, T: Clone>(self) -> Vec<T>

impl<'a, T: Clone> ImmutableCloneableVector<T> for &'a [T]

fn partitioned<'a, T: Clone>(&self, f: |&T| -> bool) -> (Vec<T>, Vec<T>)

fn permutations<'a, T: Clone>(self) -> Permutations<T>

impl<'a, T> MutableVectorAllocating<'a, T> for &'a mut [T]

fn sort_by<'a, T>(self, compare: |&T, &T| -> Ordering)

fn move_from<'a, T>(self, src: Vec<T>, start: uint, end: uint) -> uint

impl<'a, T: Ord> MutableOrdVector<T> for &'a mut [T]

fn sort<'a, T: Ord>(self)

fn next_permutation<'a, T: Ord>(self) -> bool

fn prev_permutation<'a, T: Ord>(self) -> bool

impl<'a, S: Str> StrVector for &'a [S]

fn concat<'a, S: Str>(&self) -> String

fn connect<'a, S: Str>(&self, sep: &str) -> String

impl<'a, S: Writer, T: Hash<S>> Hash<S> for &'a [T]

fn hash<'a, S: Writer, T: Hash<S>>(&self, state: &mut S)

impl<'a, S: Writer, T: Hash<S>> Hash<S> for &'a mut [T]

fn hash<'a, S: Writer, T: Hash<S>>(&self, state: &mut S)

impl<'a> BytesContainer for &'a [u8]

fn container_as_bytes<'a>(&'a self) -> &'a [u8]

fn container_into_owned_bytes(self) -> Vec<u8>

fn container_as_str<'a>(&'a self) -> Option<&'a str>

fn is_str(_: Option<Self>) -> bool

impl<'a> ToCStr for &'a [u8]

fn to_c_str<'a>(&self) -> CString

unsafe fn to_c_str_unchecked<'a>(&self) -> CString

fn with_c_str<T>(&self, f: |*i8| -> T) -> T

unsafe fn with_c_str_unchecked<T>(&self, f: |*i8| -> T) -> T

fn with_c_str<T>(&self, |*i8| -> T) -> T

unsafe fn with_c_str_unchecked<T>(&self, |*i8| -> T) -> T