| name | zig-arraylist |
| description | Reference for Zig 0.15+ ArrayList API which passes the allocator to each method call instead of at initialization. Use when working with ArrayLists in Zig 0.15+. |
Zig 0.15 ArrayList API
This project uses Zig 0.15 which has a different ArrayList API than earlier versions.
Key Differences from Older Zig Versions
Initialization
Old (pre-0.15):
var list = std.ArrayList(T).init(allocator);
New (0.15+):
var list = std.ArrayList(T){};
The allocator is NOT passed at initialization. Instead, it's passed to each method call.
Common Operations
Append:
// Old
try list.append(item);
// New (0.15)
try list.append(allocator, item);
Deinit:
// Old
list.deinit();
// New (0.15)
list.deinit(allocator);
AppendSlice:
// Old
try list.appendSlice(items);
// New (0.15)
try list.appendSlice(allocator, items);
ToOwnedSlice:
// Old
const slice = try list.toOwnedSlice();
// New (0.15)
const slice = try list.toOwnedSlice(allocator);
Complete Example
const std = @import("std");
fn example(allocator: std.mem.Allocator) ![]u32 {
// Initialize without allocator
var list = std.ArrayList(u32){};
defer list.deinit(allocator);
// All operations take allocator as first arg
try list.append(allocator, 1);
try list.append(allocator, 2);
try list.append(allocator, 3);
// Convert to owned slice
return try list.toOwnedSlice(allocator);
}
Why This Change?