[src]

Module std::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:

let int_vector = [1,2,3];
let str_vector = ["one", "two", "three"];

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

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

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):

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:

let mut numbers = ~[0, 1, 2];
numbers.push(7);
// numbers is now ~[0, 1, 2, 7];

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

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.

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

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

bytes

Operations on [u8].

raw

Unsafe operations

Chunks

An iterator over a vector in (non-overlapping) chunks (size elements at a time).

ElementSwaps

An Iterator that yields the element swaps needed to produce a sequence of all possible permutations for an indexed sequence of elements. Each permutation is only a single swap apart.

Items

Immutable slice iterator

MoveItems

An iterator that moves out of a vector.

MutChunks

An iterator over a vector in (non-overlapping) mutable chunks (size elements at a time). When the vector len is not evenly divided by the chunk size, the last slice of the iteration will be the remainder.

MutItems

Mutable slice iterator

MutSplits

An iterator over the subslices of the vector which are separated by elements that match pred.

Permutations

An Iterator that uses ElementSwaps to iterate through all possible permutations of a vector.

RevSplits

An iterator over the slices of a vector separated by elements that match a predicate function, from back to front.

Splits

An iterator over the slices of a vector separated by elements that match a predicate function.

Windows

An iterator over the (overlapping) slices of length size within a vector.

CloneableVector

Extension methods for vector slices with cloneable elements

ImmutableCloneableVector

Extension methods for vectors containing Clone elements.

ImmutableEqVector

Extension methods for vectors contain Eq elements.

ImmutableTotalOrdVector

Extension methods for vectors containing TotalOrd elements.

ImmutableVector

Extension methods for vectors

MutableCloneableVector

Trait for &[T] where T is Cloneable

MutableTotalOrdVector

Methods for mutable vectors with orderable elements, such as in-place sorting.

MutableVector

Extension methods for vectors such that their elements are mutable.

OwnedCloneableVector

Extension methods for owned vectors containing Clone elements.

OwnedEqVector

Extension methods for owned vectors containing Eq elements.

OwnedVector

Extension methods for owned vectors.

Vector

Any vector that can be represented as a slice.

VectorVector
append

Iterates over the rhs vector, copying each element and appending it to the lhs. Afterwards, the lhs is then returned for use again.

append_one

Appends one element to the vector provided. The vector itself is then returned for use again.

build

Builds a vector by calling a provided function with an argument function that pushes an element to the back of a vector. The initial capacity for the vector may optionally be specified.

from_buf

Constructs a vector from an unsafe pointer to a buffer

from_elem

Creates and initializes an owned vector.

from_fn

Creates and initializes an owned vector.

mut_ref_slice

Converts a pointer to A into a slice of length 1 (without copying).

ref_slice

Converts a pointer to A into a slice of length 1 (without copying).

unzip

Convert an iterator of pairs into a pair of vectors.

with_capacity

Creates a new vector with a capacity of capacity

RevItems
RevMoveItems

An iterator that moves out of a vector in reverse order.

RevMutItems