Skip to content

Commit e79a56f

Browse files
authored
Update Zig example code (#1737)
1 parent e4f5787 commit e79a56f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

docs/chapter_data_structure/basic_data_types.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,19 @@
173173
=== "Zig"
174174

175175
```zig title=""
176-
176+
const hello = [5]u8{ 'h', 'e', 'l', 'l', 'o' };
177+
// 以上代码展示了定义一个字面量数组的方式,其中你可以选择指明数组的大小或者使用 _ 代替。使用 _ 时,Zig 会尝试自动计算数组的长度
178+
179+
const matrix_4x4 = [4][4]f32{
180+
[_]f32{ 1.0, 0.0, 0.0, 0.0 },
181+
[_]f32{ 0.0, 1.0, 0.0, 1.0 },
182+
[_]f32{ 0.0, 0.0, 1.0, 0.0 },
183+
[_]f32{ 0.0, 0.0, 0.0, 1.0 },
184+
};
185+
// 多维数组(矩阵)实际上就是嵌套数组,我们很容易就可以创建一个多维数组出来
186+
187+
const array = [_:0]u8{ 1, 2, 3, 4 };
188+
// 定义一个哨兵终止数组,本质上来说,这是为了兼容 C 中的规定的字符串结尾字符\0。我们使用语法 [N:x]T 来描述一个元素为类型 T,长度为 N 的数组,在它对应 N 的索引处的值应该是 x
177189
```
178190

179191
??? pythontutor "可视化运行"

0 commit comments

Comments
 (0)