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 VecIterator
, 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:
Clone
Eq
, Ord
, TotalEq
, TotalOrd
-- vectors can be compared,
if the element type defines the corresponding trait.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);
}
.rev_iter()
returns an iterator with the same values as .iter()
,
but going in the reverse order, starting with the back element..mut_iter()
returns an iterator that allows modifying each value..move_iter()
converts an owned vector into an iterator that
moves out a value from the vector each iteration.There are a number of free functions that create or take vectors, for example:
from_elem
and from_fn
with_capacity
append
unzip
.bytes | Operations on |
raw | Unsafe operations |
traits |
ChunkIter | An iterator over a vector in (non-overlapping) chunks ( |
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 |
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 |
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 |
append | Iterates over the |
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 |
MutRevIterator | |
RevIterator |
Prefix searches with a type followed by a colon (e.g.
fn:
) to restrict the search to a given type.
Accepted types are: fn
, mod
,
struct
(or str
), enum
,
trait
, typedef
(or
tdef
).