std1.0.0-beta-7 SafeC docs

Module collections::vec

#include <std/collections/vec.h> source

Vec<T> (dynamic array)

struct Vec<int> v = std::Vec::new();       // T inferred from the target
v.push(3);
match (v.get(0UL)) { case null: ... case some(x): *x = 4; }
?int last = v.pop();
v.free();

Elements are stored by value in one contiguous heap buffer. Element access hands out '?&T' (null when out of range) — a reference into the buffer, valid until the next call that can reallocate it (push/insert/extend/ reserve/shrink/drop) — the compiler rejects using one after drop().

A Vec owns its buffer and its elements: it is a Drop type (drop() it, or move it, on every path), and drop()/clear() drop each element whose type owns something (String, Vec, ...: see needs_drop). push/insert/set take the element by value; one they can't store is dropped. clone(), filter(), extend() and from_arr() copy elements bit for bit, so for an element type that owns something they would share it: they panic instead (build such a copy element by element, cloning each).

Structs

Vec

Functions

vec_newkept for std/ml until its refactor moves to the static method
vec_sort_bySorts by a compile-time ordering (an Order: Less<T>, Greater<T>, or your own struct with 'int cmp(const &T, const &T) const'): every comparison is a direct call the optimizer inlines, unlike sort()'s function pointer.
vec_sortAscending natural order ('<'), comparisons inlined.
vec_mapMaps every element through 'f' into a new Vec<U>.