Module std::vec

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:

Structs

Several structs that are useful for vectors, such as VecIterator, 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):

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

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 borrowed pointers 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);
}

Function definitions

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

Modules

bytes

Operations on [u8]

raw

Unsafe operations

traits

Structs

ChunkIter

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.

MoveIterator

An iterator that moves out of a vector.

MoveRevIterator

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

Permutations

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

RSplitIterator

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

SplitIterator

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

VecIterator

An iterator for iterating over a vector.

VecMutIterator

An iterator for mutating the elements of a vector.

WindowIter

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

Traits

CopyableVector

Extension methods for vector slices with copyable elements

ImmutableCopyableVector
ImmutableEqVector
ImmutableTotalOrdVector
ImmutableVector
MutableCloneableVector

Trait for &[T] where T is Cloneable

MutableVector
OwnedCopyableVector
OwnedEqVector
OwnedVector
Vector

Any vector that can be represented as a slice.

VectorVector

Functions

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.

concat

Flattens a vector of vectors of T into a single vector of T.

concat_slices

Flattens a vector of vectors of T into a single vector of T.

connect

Concatenate a vector of vectors, placing a given separator between each

connect_slices

Concatenate a vector of vectors, placing a given separator between each

flat_map

Apply a function to each element of a vector and return a concatenation of each result vector

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.

unzip

Convert an iterator of pairs into a pair of vectors.

with_capacity

Creates a new vector with a capacity of capacity

Type Definitions

MutRevIterator
RevIterator