mirror of
https://github.com/ziglang/zig.git
synced 2024-11-26 23:22:44 +00:00
24f28753e6
Provide examples of various initializations.
29 lines
776 B
Zig
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
|