Hello everyone,
I am trying to code an iterator for a container holding a fixed size array. However, when the iterator tries to access the array values using a slice, it finds unitialized values.
Here is a minimal example of what is happening.
Test:
test "iterator" {
var container = Container.init();
var it = container.iterator();
while (it.next()) |value|
_ = value;
}
Container:
const Container = struct {
items: [3]usize,
pub fn init() Container {
return .{ .items = [3]usize{ 2, 15, 59 } };
}
pub fn iterator(self: Container) Iterator {
std.debug.print("struct: {any}\n", .{self.items});
return .{ .slice = &self.items };
}
};
Iterator:
const Iterator = struct {
slice: []const usize,
pub fn next(it: *Iterator) ?usize {
std.debug.print("iterator: {any}\n", .{it.slice});
return null;
}
};
Output:
struct: { 2, 15, 59 }
iterator: { 140731037544200, 140731037544168, 140731037544096 }
How can the iterator access the correct values?