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 that allow you to accomplish tasks with vectors, like the MutableVector
and ImmutableVector
traits.
Vectors are a very useful type, and so there's tons of implementations of traits found elsewhere. Some notable examples:
Clone
Iterator
Zero
There are a number of different functions that take vectors, here are some broad categories:
append
and grow
.bsearch
.each_permutation
.map
and partition
.push
/pop
and shift
/unshift
.head
and tail
.zip
and unzip
.And much, much more.
MutRevIterator
RevIterator
ChunkIter
- An iterator over a vector in (non-overlapping) chunks (size
elements at a time).MoveIterator
- An iterator that moves out of a vector.MoveRevIterator
- An iterator that moves out of a vector in reverse order.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.CopyableVector
- Extension methods for vector slices with copyable elementsImmutableCopyableVector
ImmutableEqVector
ImmutableTotalOrdVector
ImmutableVector
MutableCloneableVector
- Trait for &[T] where T is CloneableMutableVector
OwnedCopyableVector
OwnedEqVector
OwnedVector
Vector
- Any vector that can be represented as a slice.VectorVector
of Iterator<&'self [T]> for SplitIterator<'self, T> where <'self, T>
of Iterator<&'self [T]> for RSplitIterator<'self, T> where <'self, T>
of VectorVector<T> for &'self [~[T]] where <'self, T: Clone>
of VectorVector<T> for &'self [&'self [T]] where <'self, T: Clone>
of ::std::clone::Clone for WindowIter<'self, T> where <'self, T: ::std::clone::Clone>
- Automatically derived.of Iterator<&'self [T]> for WindowIter<'self, T> where <'self, T>
of ::std::clone::Clone for ChunkIter<'self, T> where <'self, T: ::std::clone::Clone>
- Automatically derived.of Iterator<&'self [T]> for ChunkIter<'self, T> where <'self, T>
of DoubleEndedIterator<&'self [T]> for ChunkIter<'self, T> where <'self, T>
of RandomAccessIterator<&'self [T]> for ChunkIter<'self, T> where <'self, T>
of Vector<T> for &'self [T] where <'self, T>
of Vector<T> for ~[T] where <T>
of Vector<T> for @[T] where <T>
of Container for &'self [T] where <'self, T>
of Container for ~[T] where <T>
of CopyableVector<T> for &'self [T] where <'self, T: Clone>
- Extension methods for vector slicesof CopyableVector<T> for ~[T] where <T: Clone>
- Extension methods for owned vectorsof CopyableVector<T> for @[T] where <T: Clone>
- Extension methods for managed vectorsof ImmutableVector<'self, T> for &'self [T] where <'self, T>
- Extension methods for vectorsof ImmutableEqVector<T> for &'self [T] where <'self, T: Eq>
of ImmutableTotalOrdVector<T> for &'self [T] where <'self, T: TotalOrd>
of ImmutableCopyableVector<T> for &'self [T] where <'self, T: Clone>
- Extension methods for vectorsof OwnedVector<T> for ~[T] where <T>
of Mutable for ~[T] where <T>
of OwnedCopyableVector<T> for ~[T] where <T: Clone>
of OwnedEqVector<T> for ~[T] where <T: Eq>
of MutableVector<'self, T> for &'self mut [T] where <'self, T>
of MutableCloneableVector<T> for &'self mut [T] where <'self, T: Clone>
of Clone for ~[A] where <A: Clone>
of DeepClone for ~[A] where <A: DeepClone>
of Zero for &'self [A] where <'self, A>
of Zero for ~[A] where <A>
of Zero for @[A] where <A>
of RandomAccessIterator<&'self T> for VecIterator<'self, T> where <'self, T>
of Iterator<&'self T> for VecIterator<'self, T> where <'self, T>
of DoubleEndedIterator<&'self T> for VecIterator<'self, T> where <'self, T>
of ExactSize<&'self T> for VecIterator<'self, T> where <'self, T>
of ExactSize<&'self mut T> for VecMutIterator<'self, T> where <'self, T>
of Clone for VecIterator<'self, T> where <'self, T>
of Iterator<&'self mut T> for VecMutIterator<'self, T> where <'self, T>
of DoubleEndedIterator<&'self mut T> for VecMutIterator<'self, T> where <'self, T>
of ::std::clone::Clone for MoveIterator<T> where <T: ::std::clone::Clone>
- Automatically derived.of Iterator<T> for MoveIterator<T> where <T>
of ::std::clone::Clone for MoveRevIterator<T> where <T: ::std::clone::Clone>
- Automatically derived.of Iterator<T> for MoveRevIterator<T> where <T>
of FromIterator<A> for ~[A] where <A>
of Extendable<A> for ~[A] where <A>
append
- Iterates over the rhs
vector, copying each element and appending it to the lhs
append_one
- Appends one element to the vector providedbuild
build_sized
build_sized_opt
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 eachconnect_slices
- Concatenate a vector of vectors, placing a given separator between eacheach_permutation
- Iterate over all permutations of vector v
.flat_map
- Apply a function to each element of a vector and return a concatenation of each result vectorfrom_buf
- Constructs a vector from an unsafe pointer to a bufferfrom_elem
- Creates and initializes an owned vector.from_fn
- Creates and initializes an owned vector.same_length
- Returns true if two vectors have the same lengthunzip
- Convert a vector of pairs into a pair of vectors.unzip_slice
- Convert a vector of pairs into a pair of vectors, by referencewith_capacity
- Creates a new vector with a capacity of capacity
vec::bytes
- Operations on [u8]
vec::raw
- Unsafe operationsvec::traits
MutRevIterator
type MutRevIterator<'self, T> = Invert<VecMutIterator<'self, T>>
RevIterator
type RevIterator<'self, T> = Invert<VecIterator<'self, T>>
ChunkIter
pub struct ChunkIter<'self, T> {
priv v: &'self [T],
priv size: uint,
}
An iterator over a vector in (non-overlapping) 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.
MoveIterator
pub struct MoveIterator<T> {
priv v: ~[T],
priv idx: uint,
}
An iterator that moves out of a vector.
MoveRevIterator
pub struct MoveRevIterator<T> {
priv v: ~[T],
}
An iterator that moves out of a vector in reverse order.
RSplitIterator
pub struct RSplitIterator<'self, T> {
priv v: &'self [T],
priv n: uint,
priv pred: &'self fn(t: &T) -> bool,
priv finished: bool,
}
An iterator over the slices of a vector separated by elements that match a predicate function, from back to front.
SplitIterator
pub struct SplitIterator<'self, T> {
priv v: &'self [T],
priv n: uint,
priv pred: &'self fn(t: &T) -> bool,
priv finished: bool,
}
An iterator over the slices of a vector separated by elements that match a predicate function.
VecIterator
pub struct VecIterator<'self, T> {
priv ptr: *T,
priv end: *T,
priv lifetime: &'self T,
}
An iterator for iterating over a vector.
VecMutIterator
pub struct VecMutIterator<'self, T> {
priv ptr: *mut T,
priv end: *mut T,
priv lifetime: &'self mut T,
}
An iterator for mutating the elements of a vector.
WindowIter
pub struct WindowIter<'self, T> {
priv v: &'self [T],
priv size: uint,
}
An iterator over the (overlapping) slices of length size
within a vector.
CopyableVector
Extension methods for vector slices with copyable elements
to_owned
fn to_owned(&self) -> ~[T]
Copy self
into a new owned vector
into_owned
fn into_owned(self) -> ~[T]
Convert self
into a owned vector, not making a copy if possible.
ImmutableCopyableVector
partitioned
fn partitioned(&self, f: &fn(&T) -> bool) -> (~[T], ~[T])
unsafe_get
unsafe fn unsafe_get(&self, elem: uint) -> T
ImmutableEqVector
position_elem
fn position_elem(&self, t: &T) -> Option<uint>
rposition_elem
fn rposition_elem(&self, t: &T) -> Option<uint>
contains
fn contains(&self, x: &T) -> bool
ImmutableTotalOrdVector
bsearch_elem
fn bsearch_elem(&self, x: &T) -> Option<uint>
ImmutableVector
slice
fn slice(&self, start: uint, end: uint) -> &'self [T]
slice_from
fn slice_from(&self, start: uint) -> &'self [T]
slice_to
fn slice_to(&self, end: uint) -> &'self [T]
iter
fn iter(self) -> VecIterator<'self, T>
rev_iter
fn rev_iter(self) -> RevIterator<'self, T>
split_iter
fn split_iter(self, pred: &'self fn(&T) -> bool) -> SplitIterator<'self, T>
splitn_iter
fn splitn_iter(self, n: uint, pred: &'self fn(&T) -> bool) ->
SplitIterator<'self, T>
rsplit_iter
fn rsplit_iter(self, pred: &'self fn(&T) -> bool) -> RSplitIterator<'self, T>
rsplitn_iter
fn rsplitn_iter(self, n: uint, pred: &'self fn(&T) -> bool) ->
RSplitIterator<'self, T>
window_iter
fn window_iter(self, size: uint) -> WindowIter<'self, T>
chunk_iter
fn chunk_iter(self, size: uint) -> ChunkIter<'self, T>
head
fn head(&self) -> &'self T
head_opt
fn head_opt(&self) -> Option<&'self T>
tail
fn tail(&self) -> &'self [T]
tailn
fn tailn(&self, n: uint) -> &'self [T]
init
fn init(&self) -> &'self [T]
initn
fn initn(&self, n: uint) -> &'self [T]
last
fn last(&self) -> &'self T
last_opt
fn last_opt(&self) -> Option<&'self T>
flat_map
fn flat_map<U>(&self, f: &fn(t: &T) -> ~[U]) -> ~[U]
unsafe_ref
unsafe fn unsafe_ref(&self, index: uint) -> *T
bsearch
fn bsearch(&self, f: &fn(&T) -> Ordering) -> Option<uint>
map
fn map<U>(&self, &fn(t: &T) -> U) -> ~[U]
as_imm_buf
fn as_imm_buf<U>(&self, f: &fn(*T, uint) -> U) -> U
MutableCloneableVector
Trait for &[T] where T is Cloneable
copy_from
fn copy_from(self, &[T]) -> uint
Copies as many elements from src
as it can into self
(the shorter of self.len() and src.len()). Returns the number of elements copied.
MutableVector
mut_slice
fn mut_slice(self, start: uint, end: uint) -> &'self mut [T]
mut_slice_from
fn mut_slice_from(self, start: uint) -> &'self mut [T]
mut_slice_to
fn mut_slice_to(self, end: uint) -> &'self mut [T]
mut_iter
fn mut_iter(self) -> VecMutIterator<'self, T>
mut_rev_iter
fn mut_rev_iter(self) -> MutRevIterator<'self, T>
swap
fn swap(self, a: uint, b: uint)
mut_split
fn mut_split(self, mid: uint) -> (&'self mut [T], &'self mut [T])
Divides one &mut
into two. The first will contain all indices from 0..mid
(excluding the index mid
itself) and the second will contain all indices from mid..len
(excluding the index len
itself).
reverse
fn reverse(self)
move_from
fn move_from(self, src: ~[T], start: uint, end: uint) -> uint
Consumes src
and moves as many elements as it can into self
from the range [start,end).
Returns the number of elements copied (the shorter of self.len() and end - start).
T
src
to start copying fromstr
to stop copying fromunsafe_mut_ref
unsafe fn unsafe_mut_ref(self, index: uint) -> *mut T
unsafe_set
unsafe fn unsafe_set(self, index: uint, val: T)
as_mut_buf
fn as_mut_buf<U>(self, f: &fn(*mut T, uint) -> U) -> U
OwnedCopyableVector
push_all
fn push_all(&mut self, rhs: &[T])
grow
fn grow(&mut self, n: uint, initval: &T)
grow_set
fn grow_set(&mut self, index: uint, initval: &T, val: T)
OwnedEqVector
dedup
fn dedup(&mut self)
OwnedVector
move_iter
fn move_iter(self) -> MoveIterator<T>
move_rev_iter
fn move_rev_iter(self) -> MoveRevIterator<T>
reserve
fn reserve(&mut self, n: uint)
reserve_at_least
fn reserve_at_least(&mut self, n: uint)
capacity
fn capacity(&self) -> uint
shrink_to_fit
fn shrink_to_fit(&mut self)
push
fn push(&mut self, t: T)
push_all_move
fn push_all_move(&mut self, rhs: ~[T])
pop
fn pop(&mut self) -> T
pop_opt
fn pop_opt(&mut self) -> Option<T>
shift
fn shift(&mut self) -> T
shift_opt
fn shift_opt(&mut self) -> Option<T>
unshift
fn unshift(&mut self, x: T)
insert
fn insert(&mut self, i: uint, x: T)
remove
fn remove(&mut self, i: uint) -> T
swap_remove
fn swap_remove(&mut self, index: uint) -> T
truncate
fn truncate(&mut self, newlen: uint)
retain
fn retain(&mut self, f: &fn(t: &T) -> bool)
partition
fn partition(self, f: &fn(&T) -> bool) -> (~[T], ~[T])
grow_fn
fn grow_fn(&mut self, n: uint, op: &fn(uint) -> T)
Vector
Any vector that can be represented as a slice.
as_slice
fn as_slice<'a>(&'a self) -> &'a [T]
Work with self
as a slice.
VectorVector
concat_vec
fn concat_vec(&self) -> ~[T]
connect_vec
fn connect_vec(&self, sep: &T) -> ~[T]
Iterator<&'self [T]>
for SplitIterator<'self, T>
where <'self, T>
next
fn next(&mut self) -> Option<&'self [T]>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
Iterator<&'self [T]>
for RSplitIterator<'self, T>
where <'self, T>
next
fn next(&mut self) -> Option<&'self [T]>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
VectorVector<T>
for &'self [~[T]]
where <'self, T: Clone>
concat_vec
fn concat_vec(&self) -> ~[T]
Flattens a vector of slices of T into a single vector of T.
connect_vec
fn connect_vec(&self, sep: &T) -> ~[T]
Concatenate a vector of vectors, placing a given separator between each.
VectorVector<T>
for &'self [&'self [T]]
where <'self, T: Clone>
concat_vec
fn concat_vec(&self) -> ~[T]
Flattens a vector of slices of T into a single vector of T.
connect_vec
fn connect_vec(&self, sep: &T) -> ~[T]
Concatenate a vector of slices, placing a given separator between each.
::std::clone::Clone
for WindowIter<'self, T>
where <'self, T: ::std::clone::Clone>
Automatically derived.
clone
fn clone(&self) -> WindowIter<'self, T>
Iterator<&'self [T]>
for WindowIter<'self, T>
where <'self, T>
next
fn next(&mut self) -> Option<&'self [T]>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
::std::clone::Clone
for ChunkIter<'self, T>
where <'self, T: ::std::clone::Clone>
Automatically derived.
clone
fn clone(&self) -> ChunkIter<'self, T>
Iterator<&'self [T]>
for ChunkIter<'self, T>
where <'self, T>
next
fn next(&mut self) -> Option<&'self [T]>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
DoubleEndedIterator<&'self [T]>
for ChunkIter<'self, T>
where <'self, T>
next_back
fn next_back(&mut self) -> Option<&'self [T]>
RandomAccessIterator<&'self [T]>
for ChunkIter<'self, T>
where <'self, T>
indexable
fn indexable(&self) -> uint
idx
fn idx(&self, index: uint) -> Option<&'self [T]>
Vector<T>
for &'self [T]
where <'self, T>
as_slice
fn as_slice<'a>(&'a self) -> &'a [T]
Vector<T>
for ~[T]
where <T>
as_slice
fn as_slice<'a>(&'a self) -> &'a [T]
Vector<T>
for @[T]
where <T>
as_slice
fn as_slice<'a>(&'a self) -> &'a [T]
Container
for &'self [T]
where <'self, T>
len
fn len(&self) -> uint
Returns the length of a vector
Container
for ~[T]
where <T>
len
fn len(&self) -> uint
Returns the length of a vector
CopyableVector<T>
for &'self [T]
where <'self, T: Clone>
Extension methods for vector slices
to_owned
fn to_owned(&self) -> ~[T]
Returns a copy of v
.
into_owned
fn into_owned(self) -> ~[T]
CopyableVector<T>
for ~[T]
where <T: Clone>
Extension methods for owned vectors
to_owned
fn to_owned(&self) -> ~[T]
into_owned
fn into_owned(self) -> ~[T]
CopyableVector<T>
for @[T]
where <T: Clone>
Extension methods for managed vectors
to_owned
fn to_owned(&self) -> ~[T]
into_owned
fn into_owned(self) -> ~[T]
ImmutableVector<'self, T>
for &'self [T]
where <'self, T>
Extension methods for vectors
slice
fn slice(&self, start: uint, end: uint) -> &'self [T]
Returns a slice of self between start
and end
.
Fails when start
or end
point outside the bounds of self, or when start
> end
.
slice_from
fn slice_from(&self, start: uint) -> &'self [T]
Returns a slice of self from start
to the end of the vec.
Fails when start
points outside the bounds of self.
slice_to
fn slice_to(&self, end: uint) -> &'self [T]
Returns a slice of self from the start of the vec to end
.
Fails when end
points outside the bounds of self.
iter
fn iter(self) -> VecIterator<'self, T>
rev_iter
fn rev_iter(self) -> RevIterator<'self, T>
split_iter
fn split_iter(self, pred: &'self fn(&T) -> bool) -> SplitIterator<'self, T>
Returns an iterator over the subslices of the vector which are separated by elements that match pred
.
splitn_iter
fn splitn_iter(self, n: uint, pred: &'self fn(&T) -> bool) ->
SplitIterator<'self, T>
Returns an iterator over the subslices of the vector which are separated by elements that match pred
, limited to splitting at most n
times.
rsplit_iter
fn rsplit_iter(self, pred: &'self fn(&T) -> bool) -> RSplitIterator<'self, T>
Returns an iterator over the subslices of the vector which are separated by elements that match pred
. This starts at the end of the vector and works backwards.
rsplitn_iter
fn rsplitn_iter(self, n: uint, pred: &'self fn(&T) -> bool) ->
RSplitIterator<'self, T>
Returns an iterator over the subslices of the vector which are separated by elements that match pred
limited to splitting at most n
times. This starts at the end of the vector and works backwards.
window_iter
fn window_iter(self, size: uint) -> WindowIter<'self, T>
Returns an iterator over all contiguous windows of length size
. The windows overlap. If the vector is shorter than size
, the iterator returns no values.
Fails if size
is 0.
Print the adjacent pairs of a vector (i.e. [1,2]
, [2,3]
, [3,4]
):
let v = &[1,2,3,4];
for win in v.window_iter() {
printfln!(win);
}
chunk_iter
fn chunk_iter(self, size: uint) -> ChunkIter<'self, T>
Returns an iterator over size
elements of the vector at a time. The chunks do not overlap. If size
does not divide the length of the vector, then the last chunk will not have length size
.
Fails if size
is 0.
Print the vector two elements at a time (i.e. [1,2]
, [3,4]
, [5]
):
let v = &[1,2,3,4,5];
for win in v.chunk_iter() {
printfln!(win);
}
head
fn head(&self) -> &'self T
Returns the first element of a vector, failing if the vector is empty.
head_opt
fn head_opt(&self) -> Option<&'self T>
Returns the first element of a vector, or None
if it is empty
tail
fn tail(&self) -> &'self [T]
Returns all but the first element of a vector
tailn
fn tailn(&self, n: uint) -> &'self [T]
Returns all but the first `n' elements of a vector
init
fn init(&self) -> &'self [T]
Returns all but the last element of a vector
initn
fn initn(&self, n: uint) -> &'self [T]
Returns all but the last `n' elemnts of a vector
last
fn last(&self) -> &'self T
Returns the last element of a vector, failing if the vector is empty.
last_opt
fn last_opt(&self) -> Option<&'self T>
Returns the last element of a vector, or None
if it is empty.
flat_map
fn flat_map<U>(&self, f: &fn(t: &T) -> ~[U]) -> ~[U]
Apply a function to each element of a vector and return a concatenation of each result vector
unsafe_ref
unsafe fn unsafe_ref(&self, index: uint) -> *T
Returns a pointer to the element at the given index, without doing bounds checking.
bsearch
fn bsearch(&self, f: &fn(&T) -> Ordering) -> Option<uint>
Binary search a sorted vector with a comparator function.
The comparator should implement an order consistent with the sort order of the underlying vector, returning an order code that indicates whether its argument is Less
, Equal
or Greater
the desired target.
Returns the index where the comparator returned Equal
, or None
if not found.
map
fn map<U>(&self, f: &fn(t: &T) -> U) -> ~[U]
Deprecated, use iterators where possible (self.iter().map(f)
). Apply a function to each element of a vector and return the results.
as_imm_buf
fn as_imm_buf<U>(&self, f: &fn(*T, uint) -> U) -> U
Work with the buffer of a vector.
Allows for unsafe manipulation of vector contents, which is useful for foreign interop.
ImmutableEqVector<T>
for &'self [T]
where <'self, T: Eq>
position_elem
fn position_elem(&self, x: &T) -> Option<uint>
Find the first index containing a matching value
rposition_elem
fn rposition_elem(&self, t: &T) -> Option<uint>
Find the last index containing a matching value
contains
fn contains(&self, x: &T) -> bool
Return true if a vector contains an element with the given value
ImmutableTotalOrdVector<T>
for &'self [T]
where <'self, T: TotalOrd>
bsearch_elem
fn bsearch_elem(&self, x: &T) -> Option<uint>
Binary search a sorted vector for a given element.
Returns the index of the element or None if not found.
ImmutableCopyableVector<T>
for &'self [T]
where <'self, T: Clone>
Extension methods for vectors
partitioned
fn partitioned(&self, f: &fn(&T) -> bool) -> (~[T], ~[T])
Partitions the vector into those that satisfies the predicate, and those that do not.
unsafe_get
unsafe fn unsafe_get(&self, index: uint) -> T
Returns the element at the given index, without doing bounds checking.
OwnedVector<T>
for ~[T]
where <T>
move_iter
fn move_iter(self) -> MoveIterator<T>
Creates a consuming iterator, that is, one that moves each value out of the vector (from start to end). The vector cannot be used after calling this.
Note that this performs O(n) swaps, and so move_rev_iter
(which just calls pop
repeatedly) is more efficient.
let v = ~[~"a", ~"b"];
for s in v.move_iter() {
// s has type ~str, not &~str
println(s);
}
move_rev_iter
fn move_rev_iter(self) -> MoveRevIterator<T>
Creates a consuming iterator that moves out of the vector in reverse order. Also see move_iter
, however note that this is more efficient.
reserve
fn reserve(&mut self, n: uint)
Reserves capacity for exactly n
elements in the given vector.
If the capacity for self
is already equal to or greater than the requested capacity, then no action is taken.
reserve_at_least
fn reserve_at_least(&mut self, n: uint)
Reserves capacity for at least n
elements in the given vector.
This function will over-allocate in order to amortize the allocation costs in scenarios where the caller may need to repeatedly reserve additional space.
If the capacity for self
is already equal to or greater than the requested capacity, then no action is taken.
capacity
fn capacity(&self) -> uint
Returns the number of elements the vector can hold without reallocating.
shrink_to_fit
fn shrink_to_fit(&mut self)
Shrink the capacity of the vector to match the length
push
fn push(&mut self, t: T)
Append an element to a vector
push_all_move
fn push_all_move(&mut self, mut rhs: ~[T])
Takes ownership of the vector rhs
, moving all elements into the current vector. This does not copy any elements, and it is illegal to use the rhs
vector after calling this method (because it is moved here).
let mut a = ~[~1];
a.push_all_move(~[~2, ~3, ~4]);
assert!(a == ~[~1, ~2, ~3, ~4]);
pop_opt
fn pop_opt(&mut self) -> Option<T>
Remove the last element from a vector and return it, or None
if it is empty
pop
fn pop(&mut self) -> T
Remove the last element from a vector and return it, failing if it is empty
shift
fn shift(&mut self) -> T
Removes the first element from a vector and return it
shift_opt
fn shift_opt(&mut self) -> Option<T>
Removes the first element from a vector and return it, or None
if it is empty
unshift
fn unshift(&mut self, x: T)
Prepend an element to the vector
insert
fn insert(&mut self, i: uint, x: T)
Insert an element at position i within v, shifting all elements after position i one position to the right.
remove
fn remove(&mut self, i: uint) -> T
Remove and return the element at position i within v, shifting all elements after position i one position to the left.
swap_remove
fn swap_remove(&mut self, index: uint) -> T
Remove an element from anywhere in the vector and return it, replacing it with the last element. This does not preserve ordering, but is O(1).
Fails if index >= length.
truncate
fn truncate(&mut self, newlen: uint)
Shorten a vector, dropping excess elements.
retain
fn retain(&mut self, f: &fn(t: &T) -> bool)
Like filter()
, but in place. Preserves order of v
. Linear time.
partition
fn partition(self, f: &fn(&T) -> bool) -> (~[T], ~[T])
Partitions the vector into those that satisfies the predicate, and those that do not.
grow_fn
fn grow_fn(&mut self, n: uint, op: &fn(uint) -> T)
Expands a vector in place, initializing the new elements to the result of a function
Function init_op
is called n
times with the values [0..n
)
Mutable
for ~[T]
where <T>
clear
fn clear(&mut self)
Clear the vector, removing all values.
OwnedCopyableVector<T>
for ~[T]
where <T: Clone>
push_all
fn push_all(&mut self, rhs: &[T])
Iterates over the slice rhs
, copies each element, and then appends it to the vector provided v
. The rhs
vector is traversed in-order.
let mut a = ~[1];
a.push_all([2, 3, 4]);
assert!(a == ~[1, 2, 3, 4]);
grow
fn grow(&mut self, n: uint, initval: &T)
Expands a vector in place, initializing the new elements to a given value
grow_set
fn grow_set(&mut self, index: uint, initval: &T, val: T)
Sets the value of a vector element at a given index, growing the vector as needed
Sets the element at position index
to val
. If index
is past the end of the vector, expands the vector by replicating initval
to fill the intervening space.
OwnedEqVector<T>
for ~[T]
where <T: Eq>
dedup
fn dedup(&mut self)
Remove consecutive repeated elements from a vector; if the vector is sorted, this removes all duplicates.
MutableVector<'self, T>
for &'self mut [T]
where <'self, T>
mut_slice
fn mut_slice(self, start: uint, end: uint) -> &'self mut [T]
Return a slice that points into another slice.
mut_slice_from
fn mut_slice_from(self, start: uint) -> &'self mut [T]
Returns a slice of self from start
to the end of the vec.
Fails when start
points outside the bounds of self.
mut_slice_to
fn mut_slice_to(self, end: uint) -> &'self mut [T]
Returns a slice of self from the start of the vec to end
.
Fails when end
points outside the bounds of self.
mut_split
fn mut_split(self, mid: uint) -> (&'self mut [T], &'self mut [T])
mut_iter
fn mut_iter(self) -> VecMutIterator<'self, T>
mut_rev_iter
fn mut_rev_iter(self) -> MutRevIterator<'self, T>
swap
fn swap(self, a: uint, b: uint)
Swaps two elements in a vector
reverse
fn reverse(self)
Reverse the order of elements in a vector, in place
move_from
fn move_from(self, mut src: ~[T], start: uint, end: uint) -> uint
unsafe_mut_ref
unsafe fn unsafe_mut_ref(self, index: uint) -> *mut T
unsafe_set
unsafe fn unsafe_set(self, index: uint, val: T)
as_mut_buf
fn as_mut_buf<U>(self, f: &fn(*mut T, uint) -> U) -> U
Similar to as_imm_buf
but passing a *mut T
MutableCloneableVector<T>
for &'self mut [T]
where <'self, T: Clone>
copy_from
fn copy_from(self, src: &[T]) -> uint
Clone
for ~[A]
where <A: Clone>
clone
fn clone(&self) -> ~[A]
DeepClone
for ~[A]
where <A: DeepClone>
deep_clone
fn deep_clone(&self) -> ~[A]
Zero
for &'self [A]
where <'self, A>
zero
fn zero() -> &'self [A]
is_zero
fn is_zero(&self) -> bool
Zero
for ~[A]
where <A>
zero
fn zero() -> ~[A]
is_zero
fn is_zero(&self) -> bool
Zero
for @[A]
where <A>
zero
fn zero() -> @[A]
is_zero
fn is_zero(&self) -> bool
RandomAccessIterator<&'self T>
for VecIterator<'self, T>
where <'self, T>
indexable
fn indexable(&self) -> uint
idx
fn idx(&self, index: uint) -> Option<&'self T>
Iterator<&'self T>
for VecIterator<'self, T>
where <'self, T>
next
fn next(&mut self) -> Option<&'self T>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
DoubleEndedIterator<&'self T>
for VecIterator<'self, T>
where <'self, T>
next_back
fn next_back(&mut self) -> Option<&'self T>
ExactSize<&'self T>
for VecIterator<'self, T>
where <'self, T>
ExactSize<&'self mut T>
for VecMutIterator<'self, T>
where <'self, T>
Clone
for VecIterator<'self, T>
where <'self, T>
clone
fn clone(&self) -> VecIterator<'self, T>
Iterator<&'self mut T>
for VecMutIterator<'self, T>
where <'self, T>
next
fn next(&mut self) -> Option<&'self mut T>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
DoubleEndedIterator<&'self mut T>
for VecMutIterator<'self, T>
where <'self, T>
next_back
fn next_back(&mut self) -> Option<&'self mut T>
::std::clone::Clone
for MoveIterator<T>
where <T: ::std::clone::Clone>
Automatically derived.
clone
fn clone(&self) -> MoveIterator<T>
Iterator<T>
for MoveIterator<T>
where <T>
next
fn next(&mut self) -> Option<T>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
::std::clone::Clone
for MoveRevIterator<T>
where <T: ::std::clone::Clone>
Automatically derived.
clone
fn clone(&self) -> MoveRevIterator<T>
Iterator<T>
for MoveRevIterator<T>
where <T>
next
fn next(&mut self) -> Option<T>
size_hint
fn size_hint(&self) -> (uint, Option<uint>)
FromIterator<A>
for ~[A]
where <A>
from_iterator
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> ~[A]
Extendable<A>
for ~[A]
where <A>
extend
fn extend<T: Iterator<A>>(&mut self, iterator: &mut T)
append
fn append<T: Clone>(lhs: ~[T], rhs: &[T]) -> ~[T]
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
fn append_one<T>(lhs: ~[T], x: T) -> ~[T]
Appends one element to the vector provided. The vector itself is then returned for use again.
build
fn build<A>(builder: &fn(push: &fn(v: A))) -> ~[A]
Builds a vector by calling a provided function with an argument function that pushes an element to the back of a vector.
build_sized
fn build_sized<A>(size: uint, builder: &fn(push: &fn(v: A))) -> ~[A]
Builds a vector by calling a provided function with an argument function that pushes an element to the back of a vector. This version takes an initial capacity for the vector.
build_sized_opt
fn build_sized_opt<A>(size: Option<uint>, builder: &fn(push: &fn(v: A))) ->
~[A]
Builds a vector by calling a provided function with an argument function that pushes an element to the back of a vector. This version takes an initial size for the vector.
concat
fn concat<T: Clone>(v: &[~[T]]) -> ~[T]
Flattens a vector of vectors of T into a single vector of T.
concat_slices
fn concat_slices<T: Clone>(v: &[&[T]]) -> ~[T]
Flattens a vector of vectors of T into a single vector of T.
connect
fn connect<T: Clone>(v: &[~[T]], sep: &T) -> ~[T]
Concatenate a vector of vectors, placing a given separator between each
connect_slices
fn connect_slices<T: Clone>(v: &[&[T]], sep: &T) -> ~[T]
Concatenate a vector of vectors, placing a given separator between each
each_permutation
fn each_permutation<T: Clone>(values: &[T], fun: &fn(perm: &[T]) -> bool) ->
bool
Iterate over all permutations of vector v
.
Permutations are produced in lexicographic order with respect to the order of elements in v
(so if v
is sorted then the permutations are lexicographically sorted).
The total number of permutations produced is v.len()!
. If v
contains repeated elements, then some permutations are repeated.
See Algorithms to generate permutations.
# Arguments
values
- A vector of values from which the permutations are chosen
fun
- The function to iterate over the combinations
flat_map
fn flat_map<T, U>(v: &[T], f: &fn(t: &T) -> ~[U]) -> ~[U]
Apply a function to each element of a vector and return a concatenation of each result vector
from_buf
unsafe fn from_buf<T>(ptr: *T, elts: uint) -> ~[T]
Constructs a vector from an unsafe pointer to a buffer
T
from_elem
fn from_elem<T: Clone>(n_elts: uint, t: T) -> ~[T]
Creates and initializes an owned vector.
Creates an owned vector of size n_elts
and initializes the elements to the value t
.
from_fn
fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> ~[T]
Creates and initializes an owned vector.
Creates an owned vector of size n_elts
and initializes the elements to the value returned by the function op
.
same_length
fn same_length<T, U>(xs: &[T], ys: &[U]) -> bool
Returns true if two vectors have the same length
unzip
fn unzip<T, U>(v: ~[(T, U)]) -> (~[T], ~[U])
Convert a vector of pairs into a pair of vectors.
Returns a tuple containing two vectors where the i-th element of the first vector contains the first element of the i-th tuple of the input vector, and the i-th element of the second vector contains the second element of the i-th tuple of the input vector.
unzip_slice
fn unzip_slice<T: Clone, U: Clone>(v: &[(T, U)]) -> (~[T], ~[U])
Convert a vector of pairs into a pair of vectors, by reference. As unzip().
with_capacity
fn with_capacity<T>(capacity: uint) -> ~[T]
Creates a new vector with a capacity of capacity