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 结尾的 array5// const c: [*:0]const u32 = &[4:0]u32{1, 2, 3, 4};6是 .pointer,末端必须 0- 如何拿以上的 typeinfo?
1(my_seq: anytype)2const 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.
1fn 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
1var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);2defer arena.deinit();3const allocator = arena.allocator();4// 直接用切片引用这块儿分配5const avg: []f64 = try allocator.alloc(f64, 5);6// 然后当普通切片使用,例如:7avg[2] = ...