有没有办法在Zig中拥有一个给定结构的ArrayList?

huangapple go评论60阅读模式
英文:

Is there a way to have an ArrayList of a given struct in Zig?

问题

I've thought about learning a little bit of Zig and was attempting to prototype maintaining a std.ArrayList of a struct that I created, but have been getting error{OutOfMemory}. I've tried using all the Google-Fu that I have to search for various combinations of the error and using structs with ArrayList to no avail.

This question (Malloc to a List of Struct in Zig) seems to be the closest solution, but I'd much rather things be dynamic than me having to allocate more memory than I think I'd need.

Here's the code that I have written, as a test:

const std = @import("std");

const Backtrace = struct {
  node: usize, distance: u8
};

pub fn main() void {
  const allocator = std.heap.page_allocator;
  var backtrace = std.ArrayList(Backtrace).init(allocator);
  defer backtrace.deinit();

  const nodes = [_]usize{1, 3, 5, 7, 9, 11};

  for (nodes) |n,d| {
    std.debug.print("{}: {}\n", .{n, d});
    try backtrace.append(.{.node = n, .distance = @truncate(u8, d)});
  }
}

This code was inspired by this issue (Search ArrayList of Structs in Zig).

Here's the output I get if I run this in the terminal:

$> zig run allocations.zig
allocations.zig:16:5: error: expected type 'void', found 'error{OutOfMemory}'
    try backtrace.append(.{.node = n, .distance = @truncate(u8, d)});
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/zig/0.10.1/lib/zig/std/mem/Allocator.zig:10:19: note: error set declared here
pub const Error = error{OutOfMemory};
                  ^~~~~~~~~~~~~~~~~~
allocations.zig:7:15: note: function cannot return an error
pub fn main() void {
              ^~~~
referenced by:
    comptime_0: /opt/homebrew/Cellar/zig/0.10.1/lib/zig/std/start.zig:59:50
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Finally, in case it matters, I am on Zig version 0.10.1 installed with Homebrew.

英文:

I've thought about learning a little bit of Zig and was attempting to prototype maintaining a std.ArrayList of a struct that I created, but have been getting error{OutOfMemory}. I've tried using all the Google-Fu that I have to search for various combinations of the error and using structs with ArrayList to no avail.

This question (Malloc to a List of Struct in Zig) seems to be the closest solution, but I'd much rather things be dynamic than me having to allocate more memory than I think I'd need.

Here's the code that I have written, as a test:

const std = @import("std");

const Backtrace = struct {
  node: usize, distance: u8
};

pub fn main() void {
  const allocator = std.heap.page_allocator;
  var backtrace = std.ArrayList(Backtrace).init(allocator);
  defer backtrace.deinit();

  const nodes = [_]usize{1, 3, 5, 7, 9, 11};

  for (nodes) |n,d| {
    std.debug.print("{}: {}\n", .{n, d});
    try backtrace.append(.{.node = n, .distance = @truncate(u8, d)});
  }
}

This code was inspired by this issue (Search ArrayList of Structs in Zig).

Here's the output I get if I run this in the terminal:

$> zig run allocations.zig
allocations.zig:16:5: error: expected type 'void', found 'error{OutOfMemory}'
    try backtrace.append(.{.node = n, .distance = @truncate(u8, d)});
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/zig/0.10.1/lib/zig/std/mem/Allocator.zig:10:19: note: error set declared here
pub const Error = error{OutOfMemory};
                  ^~~~~~~~~~~~~~~~~~
allocations.zig:7:15: note: function cannot return an error
pub fn main() void {
              ^~~~
referenced by:
    comptime_0: /opt/homebrew/Cellar/zig/0.10.1/lib/zig/std/start.zig:59:50
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Finally, in case it matters, I am on Zig version 0.10.1 installed with Homebrew.

答案1

得分: 1

Your main function declaration doesn't allow for errors. Change void to !void:

pub fn main() !void
英文:

Your main function declaration doesn't allow for errors. Change void to !void:

pub fn main() !void

huangapple
  • 本文由 发表于 2023年4月20日 08:21:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定