Skip to main content

Array<T>

Pebble 0.3.1 · all symbols on this page are stable.

A fixed-size, indexed container. Built from a List<T> via std.array.fromList and consumed via at(i) or the index operator.

Array<T> is not the right answer to "I need fast indexed reads on a List<T>". The on-chain cost model bills List<T> indexing as effectively constant for practical indices, and std.array.fromList itself is O(n) in the list length — so the conversion almost always costs more than the reads you were optimising. See Cost and Complexity for the measured numbers.

Reach for Array<T> only when:

  • You receive one from elsewhere (an external API, a builtin) and need to read it.
  • You're doing many indexed reads against a buffer that you'll keep across many call sites and the amortised conversion is genuinely cheap.

For sequential traversal (map, foldl, filter, ...), use List<T> directly.

Methods

MethodDescription
length(): intNumber of elements. O(1).
at(i: int): TElement at index i. Fails on out-of-bounds.

Examples (one per method)

const xs: Array<int> = std.array.fromList([10, 20, 30]);

const n: int = xs.length(); // 3
const v: int = xs.at(1); // 20

Indexing

xs[i] is sugar for xs.at(i). Both are O(1).

Constructing

Array<T> has no literal form. Build one from a list:

const xs: Array<int> = std.array.fromList([1, 2, 3]);

See also

  • std.array — function-call surface
  • List<T> — what most pipelines operate on. Indexed reads on a list are already cheap; don't convert unless you have a specific reason
  • Cost and Complexity — measured numbers for list-vs-array access