Vec
class Vec<T>
Source: compiler/src/builtin_docs/prelude.hy A growable, heap-allocated vector of T values.
Vec is auto-imported. Create one with Vec::new(),
Vec::with_capacity(n), Vec::from(array), or an array literal where a
Vec<T> is expected (let v: Vec<int> = [1, 2];). Read and write
elements with v[i] and v[i] = x; an index that is negative or not less
than v.len() panics with index out of bounds.
for x in v iterates the elements in order. Rest parameters (T... xs)
arrive as a Vec<T>, and IO buffers are Vec<byte>. A Vec supports
unary - and scalar broadcast (k * v), but two Vec values cannot be
zipped with an operator; use fixed [T; N] arrays for that.
Associated functions#
| Name | Description |
|---|---|
new | Creates a new, empty vector. |
with_capacity | Creates an empty vector with room for at least n elements. |
from | Creates a vector holding a copy of the elements of the fixed array arr. |
Methods#
| Name | Description |
|---|---|
push | Appends x to the end of the vector. |
pop | Removes the last element and returns it, or None when the vector is empty. |
insert | Inserts x at index i, shifting later elements one place to the right. |
remove | Removes the element at index i and returns it, shifting later elements left. |
clear | Removes every element. |
reserve | Ensures room for at least n more elements beyond the current length. |
capacity | Returns how many elements the vector can hold before it must reallocate. |
len | Returns the number of elements in the vector. |
Trait implementations#
ArbitraryforVec<T: Arbitrary>
Examples#
use tests::assert;
let v: Vec<int> = Vec::new();
v.push(1);
v.push(2);
v[0] = 7;
let total = 0;
for x in v {
total = total + x;
}
assert!(total == 9);
assert!(v.len() == 2);
See also#
Vec::newRange: A lazy half-open rangestart..end: every value fromstartup to, but not including,end.RangeInclusive: A lazy inclusive rangestart..=end: every value fromstartup to and includingend.