std::collections::vector (v0.2.2)

Generic contiguous dynamic array with capacity growth and index-based operations.

Latest

Overview

std::collections::vector is a generic dynamic array stored in contiguous memory. It stores a data pointer, a length, and a capacity.

The module exposes the usual operations: create a vector, push and pop values, remove values, access elements, iterate, reserve capacity, shrink storage, and drop the allocation when finished.

The API stays close to memory operations. This makes it useful for compiler data structures, parser buffers, and small tools where allocation behavior should stay visible.

Source: thrustc/std/v0.2.2/collections/vector.thrust

Public Signatures

Exported declarations for this module snapshot.

const INITIAL_CAPACITY: usize @public = 4;
struct Vector [T] @public {
struct VectorIterator [T] @public {
fn newVector [T] () Vector[T] @public;
fn withCapacity [T] (capacity: usize) Vector[T] @public;
fn dropVector [T] (vector: ptr[Vector[T]]) void @public @deallocator;
fn getLength [T] (vector: ptr[Vector[T]]) usize @public;
fn getCapacity [T] (vector: ptr[Vector[T]]) usize @public;
fn isEmpty [T] (vector: ptr[Vector[T]]) bool @public;
fn getDataPointer [T] (vector: ptr[Vector[T]]) ptr[T] @public;
fn at [T] (vector: ptr[Vector[T]], index: usize) T @public;
fn front [T] (vector: ptr[Vector[T]]) T @public;
fn back [T] (vector: ptr[Vector[T]]) T @public;
fn ensureCapacity [T] (vector: ptr[Vector[T]], minimum: usize) void @public;
fn push [T] (vector: ptr[Vector[T]], value: T) void @public;
fn pop [T] (vector: ptr[Vector[T]]) T @public;
fn insertAt [T] (vector: ptr[Vector[T]], index: usize, value: T) void @public;
fn removeAt [T] (vector: ptr[Vector[T]], index: usize) T @public;
fn clear [T] (vector: ptr[Vector[T]]) void @public;
fn reserveCapacity [T] (vector: ptr[Vector[T]], newCapacity: usize) void @public;
fn shrinkToFit [T] (vector: ptr[Vector[T]]) void @public;
fn swapElements [T] (vector: ptr[Vector[T]], first: usize, second: usize) void @public;
fn iter [T] (vector: ptr[Vector[T]]) VectorIterator[T] @public;
fn next [T] (iterator: ptr[VectorIterator[T]]) optional::Optional[T] @public;
fn forEach [T] (vector: ptr[Vector[T]], action: Fn[ptr[T], ptr, usize] -> void) void @public @arbitraryArgs;
fn filter [T] (vector: ptr[Vector[T]], predicate: Fn[T] -> bool) Vector[T] @public;
fn map [T, U] (vector: ptr[Vector[T]], mapper: Fn[T] -> U) Vector[U] @public;

Behavior and Use

Use newVector when you do not know the final size. Use withCapacity when you can reserve space up front.

Call dropVector when the vector is no longer needed. The vector owns heap memory once it grows or is created with capacity.

Examples

import std::collections::vector;
import std::io;

// Collect a few scores and report the last one entered.
fn main() s32 @public {
    var scores := vector::newVector[s32]();

    vector::push[s32](ref scores, 10);
    vector::push[s32](ref scores, 20);
    vector::push[s32](ref scores, 30);

    io::print("latest score: %d\n", vector::back[s32](ref scores));

    vector::dropVector[s32](ref scores);

    return 0;
}
import std::collections::vector;
import std::io;

// Insert at the front and then drop the first element.
fn main() s32 @public {
    var scores := vector::withCapacity[s32](4);

    vector::insertAt[s32](ref scores, 0, 5);
    vector::insertAt[s32](ref scores, 1, 8);
    vector::removeAt[s32](ref scores, 0);

    io::print("first score: %d\n", vector::front[s32](ref scores));

    vector::dropVector[s32](ref scores);

    return 0;
}
import std::collections::vector;
import std::io;

directive "--disable-warnings=W0020";

// Apply a calibration curve (value * gain + bias) to every reading, using the
// extra arguments forwarded through forEach.
static mut calibrated: s32 = 0;

fn calibrate(value: ptr[s32], args: ptr, count: usize) void {
    var gain: s32 = arbitraryArgFrom(args, s32);
    var bias: s32 = arbitraryArgFrom(args, s32);

    calibrated = calibrated + value->[0] * gain + bias;
}

fn main() s32 @public {
    var readings := vector::newVector[s32]();

    vector::push[s32](ref readings, 21);
    vector::push[s32](ref readings, 19);
    vector::push[s32](ref readings, 23);

    vector::forEach(ref readings, calibrate, 2 as s32, 5 as s32);

    io::print("calibrated total: %d\n", calibrated);

    vector::dropVector[s32](ref readings);

    return 0;
}

Notes

Back to std index