Collections
Array Slices
Array slices (T[]) are a view into contiguous data with a pointer and length:
i32 main() {
i32[] nums = [10, 20, 30];
return nums.length; // 3
}Index Access
i32 main() {
i32[] nums = [10, 20, 30];
return nums[1]; // 20
}Index Write
i32 main() {
i32[] nums = [1, 2, 3];
nums[0] = 99;
return nums[0]; // 99
}Iteration
i32 main() {
i32[] nums = [10, 20, 30, 40];
mut i32 sum = 0;
for (mut u32 i = 0; i < nums.length; i = i + 1u) {
sum = sum + nums[i];
}
return sum; // 100
}Typed Array Literals
You can explicitly specify the element type:
i32[] nums = i32[10, 20, 30];Fixed-Size Arrays
Use type[length] to allocate a fixed-size array on the stack. Returns a pointer to the first element:
i32 main() {
i8* buf = i8[64]; // 64 bytes on the stack
buf[0] = 42;
return (i32)buf[0]; // 42
}The length can be a compile-time constant:
constexpr i32 SIZE = 8192;
i32 main() {
i8* buffer = i8[SIZE];
buffer[0] = 1;
buffer[8191] = 99;
return (i32)buffer[8191]; // 99
}Fixed-size arrays with integer types:
i32 main() {
i32* data = i32[10];
for (mut i32 i = 0; i < 10; i = i + 1) {
data[i] = i * 10;
}
return data[3]; // 30
}Stack vs Heap
type[length]always allocates on the stack (no malloc/free needed)- For heap allocation, use
newwith a constructor ormallocvia extern - Inside
asyncfunctions, the compiler automatically promotes stack allocations to the coroutine frame on the heap
array<T>
A generic, dynamically-sized array collection. Manages its own heap memory with automatic growth. Requires T : Hashable.
import std::collections;
struct array<T> : Hashable
where T : Hashable {
T* data;
size length;
size capacity;
}Constructor
array<i32> nums = array<i32>();Methods
Method
Description
push(T value)Appends an element to the end. Grows capacity automatically.get(size index) → TReturns the element at the given index. Asserts bounds.set(size index, T value)Replaces the element at the given index. Asserts bounds.length() → sizeReturns the number of elements.reserve(size capacity)Pre-allocates memory for at least the given capacity.hash() → u64Computes a hash over all elements.destroy()Frees the underlying memory.Example
import std::collections;
i32 main() {
array<i32> nums = array<i32>();
nums.reserve(16u);
nums.push(10);
nums.push(20);
nums.push(30);
i32 second = nums.get(1u); // 20
nums.set(0u, 99);
nums.destroy();
return second;
}map<Key, Value>
A generic hash map using open addressing with linear probing. Requires Key : Hashable, Equatable and Value : Hashable, Equatable.
import std::collections;
struct map<Key, Value>
where Key : Hashable, Equatable;
where Value : Hashable, Equatable
{
Key* keys;
Value* values;
i8* states;
size count;
size capacity;
}Constructor
map<i32, i32> m = map<i32, i32>(); // initial capacity: 16Methods
Method
Description
set(Key key, Value value)Inserts or updates the value for the given key. Grows at 75% load factor.get(Key key) → ValueReturns the value for the given key. Asserts if key not found.has(Key key) → boolReturns true if the key exists in the map.remove(Key key) → boolRemoves the entry for the given key. Returns true if it existed.length() → sizeReturns the number of entries.hash() → u64Computes a hash over all keys.destroy()Frees all underlying memory (keys, values, states).Example
import std::collections;
i32 main() {
map<i32, i32> scores = map<i32, i32>();
scores.set(1, 100);
scores.set(2, 200);
scores.set(3, 300);
bool exists = scores.has(2); // true
i32 val = scores.get(1); // 100
scores.remove(3);
scores.destroy();
return val;
}range
Represents a numeric range with configurable bounds and step. Created implicitly by range-for syntax or explicitly.
struct range {
i32 start;
i32 end;
i32 step;
u1 start_inclusive;
u1 end_inclusive;
}Methods
Method
Description
length() → i32Returns the number of elements in the range.is_empty() → boolReturns true if the range contains no elements.Range-for Syntax
Ranges are most commonly used in for loops:
for (i32 i in 0..10) { } // 0 to 9 (exclusive end)
for (i32 i in 0..=10) { } // 0 to 10 (inclusive end)
for (i32 i in [0..10]) { } // 0 to 9, closed start
for (i32 i in [0..10)) { } // 0 to 9, half-open