Skip to content
Chung Leong edited this page Feb 11, 2024 · 12 revisions

In Zig, arrays can contain only a single type of values. Their lengths are fixed at compile time.

const std = @import("std");

pub fn print4(values: i32) void {
    std.debug.print("{d}\n", .{values});
}
import { print4 } from './array-example-1.zig';

print4([ 123, 234, 345, 456 ]);
123, 234, 345, 456

Array objects support bracket operator and iterator.

pub fn get4(offset: i32) [4]i32 {
    var values: [4]32;
    for (values, 0..) |*value_ptr, index| {
        value_ptr.* = index + offset;
    }
    return values;
}
import { get4 } from './array-example-2.zig';

const array = get4(80);
console.log(array[3]);
for (const value of array) {
    console.log(value);
}
console.log([ ...array ]);

80 80 81 82 83 80, 81, 82, 83

Clone this wiki locally