Arrays

    1. Test [1/4] test "iterate over an array"...
    2. Test [2/4] test "modify an array"...
    3. Test [3/4] test "compile-time array initialization"...
    4. Test [4/4] test "array initialization with function calls"...
    5. All 4 tests passed.

    See also:

    Similar to and Anonymous Struct Literals the type can be omitted from array literals:

    1. const std = @import("std");
    2. const expect = std.testing.expect;
    3. test "anonymous list literal syntax" {
    4. var array: [4]u8 = .{11, 22, 33, 44};
    5. try expect(array[0] == 11);
    6. try expect(array[2] == 33);
    7. try expect(array[3] == 44);
    8. }

    If there is no type in the result location then an anonymous list literal actually turns into a with numbered field names:

    infer_list_literal.zig

    1. const expect = std.testing.expect;
    2. test "fully anonymous list literal" {
    3. try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"});
    4. }
    5. fn dump(args: anytype) !void {
    6. try expect(args.@"0" == 1234);
    7. try expect(args.@"1" == 12.34);
    8. try expect(args.@"2");
    9. try expect(args.@"3"[0] == 'h');
    10. try expect(args.@"3"[1] == 'i');
    11. }
    1. $ zig test infer_list_literal.zig

    multidimensional.zig

    1. $ zig test multidimensional.zig
    2. Test [1/1] test "multidimensional arrays"...
    3. All 1 tests passed.

    The syntax [N:x]T describes an array which has a sentinel element of value x at the index corresponding to len.

    1. const std = @import("std");
    2. const expect = std.testing.expect;
    3. test "null terminated array" {
    4. const array = [_:0]u8 {1, 2, 3, 4};
    5. try expect(@TypeOf(array) == [4:0]u8);
    6. try expect(array.len == 4);
    7. try expect(array[4] == 0);

    See also: