跳转到内容
语言参考Zig 0.17.0官方原文
test_arrays.zig
const expectEqual = @import("std").testing.expectEqual;
const assert = @import("std").debug.assert;
const mem = @import("std").mem;
// 数组字面量
const message = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
// 使用结果位置的另一种初始化方式
const alt_message: [5]u8 = .{ 'h', 'e', 'l', 'l', 'o' };
comptime {
assert(mem.eql(u8, &message, &alt_message));
}
// 获取数组大小
comptime {
assert(message.len == 5);
}
// 字符串字面量是指向数组的单项指针。
const same_message = "hello";
comptime {
assert(mem.eql(u8, &message, same_message));
}
test "iterate over an array" {
var sum: usize = 0;
for (message) |byte| {
sum += byte;
}
try expectEqual('h' + 'e' + 'l' * 2 + 'o', sum);
}
// 可修改的数组
var some_integers: [100]i32 = undefined;
test "modify an array" {
for (&some_integers, 0..) |*item, i| {
item.* = @intCast(i);
}
try expectEqual(10, some_integers[10]);
try expectEqual(99, some_integers[99]);
}
// 如果值在编译时已知,
// 就可以拼接数组
const part_one = [_]i32{ 1, 2, 3, 4 };
const part_two = [_]i32{ 5, 6, 7, 8 };
const all_of_it = part_one ++ part_two;
comptime {
assert(mem.eql(i32, &all_of_it, &[_]i32{ 1, 2, 3, 4, 5, 6, 7, 8 }));
}
// 记住,字符串字面量表示数组
const hello = "hello";
const world = "world";
const hello_world = hello ++ " " ++ world;
comptime {
assert(mem.eql(u8, hello_world, "hello world"));
}
// 将数组初始化为零
const all_zero: [10]u16 = @splat(0);
comptime {
assert(all_zero.len == 10);
assert(all_zero[5] == 0);
}
// 使用编译期代码初始化数组
var fancy_array = init: {
var initial_value: [10]Point = undefined;
for (&initial_value, 0..) |*pt, i| {
pt.* = Point{
.x = @intCast(i),
.y = @intCast(i * 2),
};
}
break :init initial_value;
};
const Point = struct {
x: i32,
y: i32,
};
test "compile-time array initialization" {
try expectEqual(4, fancy_array[4].x);
try expectEqual(8, fancy_array[4].y);
}
// 调用函数初始化数组
var more_points: [10]Point = @splat(makePoint(3));
fn makePoint(x: i32) Point {
return Point{
.x = x,
.y = x * 2,
};
}
test "array initialization with function calls" {
try expectEqual(3, more_points[4].x);
try expectEqual(6, more_points[4].y);
try expectEqual(10, more_points.len);
}
命令行
$ zig test test_arrays.zig
1/4 test_arrays.test.iterate over an array...OK
2/4 test_arrays.test.modify an array...OK
3/4 test_arrays.test.compile-time array initialization...OK
4/4 test_arrays.test.array initialization with function calls...OK
All 4 tests passed.

另见:

多维数组

通过嵌套数组可以创建多维数组:

test_multidimensional_arrays.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;
const mat4x5 = [4][5]f32{
[_]f32{ 1.0, 0.0, 0.0, 0.0, 0.0 },
[_]f32{ 0.0, 1.0, 0.0, 1.0, 0.0 },
[_]f32{ 0.0, 0.0, 1.0, 0.0, 0.0 },
[_]f32{ 0.0, 0.0, 0.0, 1.0, 9.9 },
};
test "multidimensional arrays" {
// mat4x5 本身是一个元素为数组的一维数组。
try expectEqual(mat4x5[1], [_]f32{ 0.0, 1.0, 0.0, 1.0, 0.0 });
// 先索引外层数组,再索引内层数组,即可访问二维数组。
try expectEqual(9.9, mat4x5[3][4]);
// 此处使用 for 循环迭代。
for (mat4x5, 0..) |row, row_index| {
for (row, 0..) |cell, column_index| {
if (row_index == column_index) {
try expectEqual(1.0, cell);
}
}
}
// 将多维数组初始化为零。
const all_zero: [4][5]f32 = @splat(@splat(0));
try expectEqual(0, all_zero[0][0]);
}
命令行
$ zig test test_multidimensional_arrays.zig
1/1 test_multidimensional_arrays.test.multidimensional arrays...OK
All 1 tests passed.

哨兵终止数组

语法 [N:x]T 表示一个数组,其长度 N 对应的索引位置额外包含值为 x 的哨兵元素。

test_null_terminated_array.zig
const std = @import("std");
const expectEqual = std.testing.expectEqual;
test "0-terminated sentinel array" {
const array = [_:0]u8{ 1, 2, 3, 4 };
try expectEqual([4:0]u8, @TypeOf(array));
try expectEqual(4, array.len);
try expectEqual(0, array[4]);
}
test "extra 0s in 0-terminated sentinel array" {
// 哨兵值可以提前出现,但不会影响编译时的 'len'。
const array = [_:0]u8{ 1, 0, 0, 4 };
try expectEqual([4:0]u8, @TypeOf(array));
try expectEqual(4, array.len);
try expectEqual(0, array[4]);
}
命令行
$ zig test test_null_terminated_array.zig
1/2 test_null_terminated_array.test.0-terminated sentinel array...OK
2/2 test_null_terminated_array.test.extra 0s in 0-terminated sentinel array...OK
All 2 tests passed.

另见:

数组解构

数组可以解构:

destructuring_arrays.zig
const print = @import("std").debug.print;
fn swizzleRgbaToBgra(rgba: [4]u8) [4]u8 {
// 通过解构实现易读的分量重排
const r, const g, const b, const a = rgba;
return .{ b, g, r, a };
}
pub fn main() void {
const pos = [_]i32{ 1, 2 };
const x, const y = pos;
print("x = {}, y = {}\n", .{x, y});
const orange: [4]u8 = .{ 255, 165, 0, 255 };
print("{any}\n", .{swizzleRgbaToBgra(orange)});
}
命令行
$ zig build-exe destructuring_arrays.zig
$ ./destructuring_arrays
x = 1, y = 2
{ 0, 165, 255, 255 }

另见: