Class in prelude auto-imported

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#

NameDescription
newCreates a new, empty vector.
with_capacityCreates an empty vector with room for at least n elements.
fromCreates a vector holding a copy of the elements of the fixed array arr.

Methods#

NameDescription
pushAppends x to the end of the vector.
popRemoves the last element and returns it, or None when the vector is empty.
insertInserts x at index i, shifting later elements one place to the right.
removeRemoves the element at index i and returns it, shifting later elements left.
clearRemoves every element.
reserveEnsures room for at least n more elements beyond the current length.
capacityReturns how many elements the vector can hold before it must reallocate.
lenReturns the number of elements in the vector.

Trait implementations#

  • Arbitrary for Vec<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::new
  • Range: A lazy half-open range start..end: every value from start up to, but not including, end.
  • RangeInclusive: A lazy inclusive range start..=end: every value from start up to and including end.
↑ ↓ navigate ↵ open