How to?

ziglings

Mar 5, 2026
langszig
1 Minutes
160 Words

sentinels (f10:45)

1
// const a: [4:0]u32 = [4:0]u32{1, 2, 3, 4};
2
是 .array, 额外存储 0 和长度
3
// const b: [:0]const u32 = &[4:0]u32{1, 2, 3, 4};
4
是 .slice,必须指向 0 结尾的 array
5
// const c: [*:0]const u32 = &[4:0]u32{1, 2, 3, 4};
6
是 .pointer,末端必须 0
  • 如何拿以上的 typeinfo?
1
(my_seq: anytype)
2
const my_typeinfo = @typeInfo(@TypeOf(my_seq));
3
switch (my_typeinfo) {
4
.array => {
5
...
6
},
7
.pointer => {
8
...
9
},
10
else => unreachable,

anonymouse_strcuts3

  • 如何遍历任何 struct 的 fields.
1
fn printTuple(tuple: anytype) void {
2
const fields = @typeInfo(@TypeOf(tuple)).@"struct".fields;
3
inline for (fields) |field| {
4
print("\"{s}\"({any}):{any} ", .{
5
field.name,
6
field.type,
7
@field(tuple, field.name)
8
});
9
}

memory_allocation

1
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
2
defer arena.deinit();
3
const allocator = arena.allocator();
4
// 直接用切片引用这块儿分配
5
const avg: []f64 = try allocator.alloc(f64, 5);
6
// 然后当普通切片使用,例如:
7
avg[2] = ...
Article title:ziglings
Article author:Julyfun
Release time:Mar 5, 2026
Copyright 2026
Sitemap