Back to course

Arrays and Slices

Full Course: Zig Programming From Zero to Hero

Arrays and Slices

Arrays

Arrays have a fixed size known at compile time.

zig const array = [5]i32{ 1, 2, 3, 4, 5 }; const inferred_size = [_]i32{ 1, 2, 3 };

Slices

A slice is a pointer and a length. It allows you to view a portion of an array.

zig const slice: []const i32 = array[1..3]; // slice[0] is array[1]

Slices are common in Zig because they provide bounds checking at runtime (in debug mode).