Collections
Arrays
The length of an array must be known at comptime. If that’s not possible, a slice should be used.
- Type
[len]Type, or with a sentinel:[len:sentinel]Type- Create an array
[_]Type{ a, b, c }- Length
arr.len- Index
arr[idx]- Destructure
const a, const b = [_]i32{ 1, 2 };- Comptime concat
arr1 ++ arr2- Comptime repeat
arr ** int
Cookbook
const arr = [_]i32{ 1, 2, 4, 8, 16 };
for (arr) |v| {
doThings(v);
}var arr = [_]i32{ 1, 2, 4, 8, 16 };
for &arr |*v| {
v.* *= 2;
}const arr = [_]i32{ 1, 2, 4, 8, 16 };
for (arr, 0..) |v, i| {
std.debug.print("Index {d} has {d}\n", .{v, i});
}const zeros = [_]i32{0} ** 100;Strings
String literals are actually constant pointers to a null-terminated array. Since their lengths are always known they can be coerced to slices or a null-temrinated pointer. They can also be converted to the underlying array by dereferencing them.
By convention, since Zig has no string type, functions taking a string use type []const u8.
Vectors
Vectors are like arrays, but they let you run operations in parallel on them using SIMD.
They can hold the following:
Booleans
Integers
Floats
Pointers
They support these operations:
Arithmetic (including complex arithmetic like
@sqrt)Bitwise
Comparison
Boolean not
Using type coercion they can be converted to arrays and back. Using slc.* a slice can be converted to a vector if it’s length is comptime known.
- Type
@Vector(len, Type)- Create a vector
@Vector(len, Type){ a, b, c, d }- Index
vec[idx]- Destructure
const a, const b = vec;
See also (bulitin functions):