Arrays
Test [1/4] test "iterate over an array"...
Test [2/4] test "modify an array"...
Test [3/4] test "compile-time array initialization"...
Test [4/4] test "array initialization with function calls"...
All 4 tests passed.
See also:
Similar to and Anonymous Struct Literals the type can be omitted from array literals:
const std = @import("std");
const expect = std.testing.expect;
test "anonymous list literal syntax" {
var array: [4]u8 = .{11, 22, 33, 44};
try expect(array[0] == 11);
try expect(array[2] == 33);
try expect(array[3] == 44);
}
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
const expect = std.testing.expect;
test "fully anonymous list literal" {
try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"});
}
fn dump(args: anytype) !void {
try expect(args.@"0" == 1234);
try expect(args.@"1" == 12.34);
try expect(args.@"2");
try expect(args.@"3"[0] == 'h');
try expect(args.@"3"[1] == 'i');
}
$ zig test infer_list_literal.zig
multidimensional.zig
$ zig test multidimensional.zig
Test [1/1] test "multidimensional arrays"...
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
.
const std = @import("std");
const expect = std.testing.expect;
test "null terminated array" {
const array = [_:0]u8 {1, 2, 3, 4};
try expect(@TypeOf(array) == [4:0]u8);
try expect(array.len == 4);
try expect(array[4] == 0);
See also: