Skip to content

std/vector

Operations on the immutable random-access Vector<'T> type. Use List<'T> for cons-style recursion and cheap prepends; use Vector<'T> when indexed lookup and push-at-end are the natural operations. Generic iteration still goes through the Iter module.

Import with import std/vector.

Types

type Vector<'T>

Immutable random-access sequence of elements.

fn empty(): Vector<'T>

type function on Vector

Returns an empty vector.

Interactive Tests

v: Vector<Int> = Vector.empty()
assert Vector.length(v) == 0
fn from_list(items: List<'T>): Vector<'T>

type function on Vector

Builds a vector from a list, preserving order.

Interactive Tests

assert Vector.from_list([1, 2, 3]) == #[1, 2, 3]
fn to_list(vector: Vector<'T>): List<'T>

type function on Vector

Converts a vector to a list, preserving order.

Interactive Tests

assert Vector.to_list(#[1, 2, 3]) == [1, 2, 3]
fn length(vector: Vector<'T>): Int

type function on Vector

Returns the number of elements. O(1).

Interactive Tests

assert Vector.length(#["a", "b"]) == 2
fn at(vector: Vector<'T>, index: Int): Maybe<'T>

type function on Vector

Returns the element at zero-based index, or None when out of bounds.

Interactive Tests

assert Vector.at(#["a", "b"], 1) == Some("b")
assert Vector.at(#["a", "b"], 9) == None
fn push(vector: Vector<'T>, item: 'T): Vector<'T>

type function on Vector

Returns a new vector with item appended to the end.

Interactive Tests

assert Vector.push(#[1, 2], 3) == #[1, 2, 3]
fn set(vector: Vector<'T>, index: Int, item: 'T): Maybe<Vector<'T>>

type function on Vector

Returns a new vector with item replacing the element at index, or None when the index is out of bounds.

Interactive Tests

assert Vector.set(#["a", "b"], 1, "z") == Some(#["a", "z"])
fn concat(a: Vector<'T>, b: Vector<'T>): Vector<'T>

type function on Vector

Concatenates two vectors.

Interactive Tests

assert Vector.concat(#[1, 2], #[3]) == #[1, 2, 3]
fn vector_next(vector: Vector<'T>): Maybe<('T, Vector<'T>)>

type function on Vector

Returns the next element and remaining vector, or None if empty. Drives the Iter implementation for Vector.

impl Iterable

fn next(vector: Vector<'T>): Maybe<('T, Vector<'T>)>

impl Iterable.next

impl Iterable

fn known_count(vector: Vector<'T>): Maybe<Int>

impl Iterable.known_count

impl Add

fn add(lhs: Vector<'T>, rhs: Vector<'T>): Vector<'T>

impl Add.add

impl Display

fn to_string(vector: Vector<'T>): String

impl Display.to_string

impl Debug

fn inspect(vector: Vector<'T>): String

impl Debug.inspect

impl Equatable

fn equal?(a: Vector<'T>, b: Vector<'T>): Bool

impl Equatable.equal?

impl Hashable

fn hash(vector: Vector<'T>): Int

impl Hashable.hash

impl Comparable

fn compare(a: Vector<'T>, b: Vector<'T>): Ordering

impl Comparable.compare