Vec<T>

A contiguous, growable array type. The most commonly used collection in Joule.

Construction

let mut v: Vec<i32> = Vec::new();   // empty vector

Adding Elements

v.push(1);
v.push(2);
v.push(3);

Accessing Elements

let first = v[0];          // indexing (panics if out of bounds)
let len = v.len();          // number of elements
let empty = v.is_empty();   // true if len == 0

Iteration

for item in v {
    process(item);
}

Removing Elements

let last = v.pop();         // Option<T> -- removes and returns last element

Common Patterns

Collecting Results

let mut results: Vec<i32> = Vec::new();
let mut i = 0;
while i < 10 {
    results.push(i * i);
    i = i + 1;
}

As a Stack

let mut stack: Vec<i32> = Vec::new();
stack.push(1);      // push
stack.push(2);
let top = stack.pop();  // pop -- Option::Some(2)

Memory Layout

Vec<T> {
    data: *mut T,      // pointer to heap allocation
    len: usize,        // number of elements
    capacity: usize,   // allocated capacity
}

Vec grows automatically when elements are added beyond the current capacity. Growth is amortized O(1).