Arrays
1/4 test "iterate over an array"...OK
2/4 test "modify an array"...OK
3/4 test "compile-time array initalization"...OK
4/4 test "array initialization with function calls"...OK
All 4 tests passed.
See also:
Similar to Enum Literals and the type can be omitted from array literals:
const std = @import("std");
const assert = std.debug.assert;
test "anonymous list literal syntax" {
var array: [4]u8 = .{11, 22, 33, 44};
assert(array[0] == 11);
assert(array[2] == 33);
assert(array[3] == 44);
}
If there is no type in the result location then an anonymous list literal actually turns into a struct with numbered field names:
infer_list_literal.zig
const assert = std.debug.assert;
test "fully anonymous list literal" {
dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"});
}
fn dump(args: var) void {
assert(args.@"0" == 1234);
assert(args.@"1" == 12.34);
assert(args.@"2");
assert(args.@"3"[0] == 'h');
assert(args.@"3"[1] == 'i');
}
1/1 test "fully anonymous list literal"...OK
multidimensional.zig
$ zig test multidimensional.zig
1/1 test "multidimensional arrays"...OK
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 assert = std.debug.assert;
test "null terminated array" {
const array = [_:0]u8 {1, 2, 3, 4};
assert(@TypeOf(array) == [4:0]u8);
assert(array.len == 4);
assert(array[4] == 0);
See also: