iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👻

[Zig] Adding Structs to MultiArrayList

に公開

I wrote this as a practice for HPACK encoding and decoding.

const std = @import("std");
const print = std.debug.print;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();

    const Person = struct {
        name: []const u8,
        age: u8
    };

    var list = std.MultiArrayList(Person){};
    defer list.deinit(allocator);

    try list.append(allocator, .{
        .name = "Foo",
        .age = 24,
    });
    try list.append(allocator, .{
        .name = "Bar",
        .age = 28
    });

    print("{s} {d}\n", .{ list.items(.name)[0], list.items(.age)[0] });
    print("{s} {d}\n", .{ list.items(.name)[1], list.items(.age)[1] });
}

Discussion