Module rune_alloc::vec

source ·
Expand description

A contiguous growable array type with heap-allocated contents, written Vec<T>.

Vectors have O(1) indexing, amortized O(1) push (to the end) and O(1) pop (from the end).

Vectors ensure they never allocate more than isize::MAX bytes.

§Examples

You can explicitly create a Vec with Vec::new:

use rune::alloc::Vec;

let v: Vec<i32> = Vec::new();

…or by using the try_vec! macro:

use rune::alloc::{try_vec, Vec};

let v: Vec<i32> = try_vec![];
let v = try_vec![1, 2, 3, 4, 5];
let v = try_vec![0; 10]; // ten zeroes

You can try_push values onto the end of a vector (which will grow the vector as needed):

use rune::alloc::try_vec;
let mut v = try_vec![1, 2];

v.try_push(3)?;

Popping values works in much the same way:

use rune::alloc::try_vec;

let mut v = try_vec![1, 2];

let two = v.pop();

Vectors also support indexing (through the Index and IndexMut traits):

use rune::alloc::try_vec;

let mut v = try_vec![1, 2, 3];
let three = v[2];
v[1] = v[1] + 5;

Structs§

  • A draining iterator for Vec<T>.
  • An iterator that moves out of a vector.
  • A contiguous growable array type, written as Vec<T>, short for ‘vector’.