New zig user here, using zig version 0.10.1.
I'm trying to create a parser that parse from a buffer and return a stream of items, something like below:
const buffer: []const u8 = ... // load from file
var parser = Parser.init(allocator, buffer);
var iterator = parser.parse();
while (iterator.next()) |item| {
// do something
// ! deinit the item as the item is no longer needed
}
the iterator returns an item which has additional mem allocated. I'd like to deinit it as I iterate through the result. Zig complains that the item is a const pointer, so I tried to change it to |*item|, but it doesn't work, still complains the same error.
I found a workaround but it looks awkward:
var i = iterator.next();
while (i != null) {
if (i) |*item| {
// do something
item.deinit();
}
i = iterator.next();
}
So if the |*item| syntax works for the if statement, should the syntax |*item| also work in the while loop? Is there a better way to achieve this?