小编Ton*_*nis的帖子

在 Zig lang 中分配通用结构

是否可以为struct包含类型作为属性的对象创建分配?

例如

示例结构是

const Content = struct {
    content: type,
    name: []const u8,
};
Run Code Online (Sandbox Code Playgroud)

然后,我想分配内存,例如2*Content

我知道我可以使用

const list: [2]CustomElement = .{ Content{ .content = Type, .name = "test" }, Content{ .content = Type, .name = "test" } };
Run Code Online (Sandbox Code Playgroud)

但如何使用它们来实现同样的事情allocators呢?

这是行不通的。

  comptime {
        var list = std.ArrayList(Content).init(test_allocator);
        try list.append(Content{ .content = MyType , .name = "test" });
    }
Run Code Online (Sandbox Code Playgroud)

我收到错误

error: parameter of type '*std.array_list.ArrayListAligned(Content,null)' must be declared comptime
Run Code Online (Sandbox Code Playgroud)

简而言之

Box是否可以像Rust 中那样构建功能?

allocator zig

5
推荐指数
1
解决办法
2460
查看次数

标签 统计

allocator ×1

zig ×1