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

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 values, remove values, access elements, reserve capacity, shrink storage, and destroy 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.1/collections/vector.thrust

Public Signatures

Exported declarations for this module snapshot.

const INITIAL_CAPACITY: usize @public = 4;
struct Vector [T] @public {
fn newVector [T] () Vector[T] @public;
fn withCapacity [T] (capacity: usize) Vector[T] @public;
fn destroyVector [T] (vector: ptr[Vector[T]]) void @public;
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 pushBack [T] (vector: ptr[Vector[T]], value: T) void @public;
fn popBack [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;

Behavior and Use

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

Call destroyVector 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;

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

    vector::pushBack[s32](ref v, 10);
    vector::pushBack[s32](ref v, 20);
    vector::pushBack[s32](ref v, 30);

    var last: s32 = vector::back[s32](ref v);

    vector::destroyVector[s32](ref v);

    if last == 30 {
        return 0;
    }

    return 1;
}
import std::collections::vector;

fn main() s32 @public {
    var v := vector::withCapacity[s32](4);

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

    var value: s32 = vector::front[s32](ref v);

    vector::destroyVector[s32](ref v);

    if value == 8 {
        return 0;
    }

    return 1;
}

Notes

Back to std index