zig/doc/langref/test_multidimensional_arrays.zig
Gordon Cassie 24f28753e6
Document a few non-obvious variable assignments (#20213)
Provide examples of various initializations.
2024-06-08 12:39:11 -07:00

29 lines
776 B
Zig

const std = @import("std");
const expect = std.testing.expect;
const mat4x4 = [4][4]f32{
[_]f32{ 1.0, 0.0, 0.0, 0.0 },
[_]f32{ 0.0, 1.0, 0.0, 1.0 },
[_]f32{ 0.0, 0.0, 1.0, 0.0 },
[_]f32{ 0.0, 0.0, 0.0, 1.0 },
};
test "multidimensional arrays" {
// Access the 2D array by indexing the outer array, and then the inner array.
try expect(mat4x4[1][1] == 1.0);
// Here we iterate with for loops.
for (mat4x4, 0..) |row, row_index| {
for (row, 0..) |cell, column_index| {
if (row_index == column_index) {
try expect(cell == 1.0);
}
}
}
// initialize a multidimensional array to zeros
const all_zero: [4][4]f32 = .{.{0} ** 4} ** 4;
try expect(all_zero[0][0] == 0);
}
// test