This gives me the compiler error that I cannot assign to const
const Value = struct {
x: u8
};
pub fn main() !void {
var list = std.arrayList(Value).init(allocator);
try list.append(.{.x = 1});
try list.append(.{.x = 2});
try list.append(.{.x = 3});
for (list.items) |item| {
item.x += 1;
}
}
If I iterate over the indices of the list like this, it works fine. But is there a better way of iterating over the items of the list as var instead of const?
const Value = struct {
x: u8
};
pub fn main() !void {
var list = std.arrayList(Value).init(allocator);
try list.append(.{.x = 1});
try list.append(.{.x = 2});
try list.append(.{.x = 3});
for (0..list.items.len) |i| {
list.items[i].x += 1;
}
}