3 ms·
> Zig’s requirement of explicitly passing around an Allocator type removes any ambiguity completely. Not really... var list = std.ArrayList(u21).init(std.
by veber-alex 4y ago
> Zig’s requirement of explicitly passing around an Allocator type removes any ambiguity completely.
Not really...
var list = std.ArrayList(u21).init(std.testing.allocator);
try list.append('a');
Does creating the ArrayList allocate memory?
Is appending to the array list reallocates?
The only thing you know is that ArrayList will sometime use the allocator, you can't tell when.
And this gets worse once the abstraction level increases, if you have your own type which accepts an allocator and has 100 methods it's impossible to tell which methods allocate.
In both languages you still need to know the specifics of the data structures you are working with, the only real problem that Zig solves is to actually allow you to use custom allocators with the std collections, which is something that is still unstable in Rust.
- tomck 4y agoYou can tell when, because if it uses the allocator it will return an error. So the first line definitely doesn't allocate, and the second definitely does. That is, unless you explicitly handle OOM conditions inside your construct, e.g. 'crash if you're OOM', which isn't typical in zig code. All code I interact with will return an allocator error if allocation fails.