3 ms·
could you clarify for me, because isn't that a different guarantee? from my understanding, Rust and Zig make allocations explicit, but [@zero_alloc] lets you de
by scritty-dev 3mo ago
could you clarify for me, because isn't that a different guarantee? from my understanding, Rust and Zig make allocations explicit, but [@zero_alloc] lets you declare "this function (and everything it calls) must not allocate" and have compiler enforce it.
- wmedrano 3mo agoIf you don't pass an allocator param in Zig, then the function basically can't allocate.
- 3836293648 3mo agoNo, that's just a convention. Any function can just create a new allocator, or import the global one, or use malloc from libc. Zig does nothing to enforce this convention.
- scritty-dev 3mo agoisn't that more of a convention than an actual guarantee?
- eatonphil 3mo agoThey are indeed different. In Zig you might get the allocator via a struct field or just via importing the global allocator. const std = @import("std"); fn makeBuffer(n: usize) ![]u8 { return std.heap.page_allocator.alloc(u8, n); } Here are a few examples from the last release of Bun before the rewrite into Rust. https://github.com/oven-sh/bun/blob/bun-v1.3.14/src/install/dependency.zig#L447 https://github.com/oven-sh/bun/blob/bun-v1.3.14/src/install/... https://github.com/oven-sh/bun/blob/bun-v1.3.14/src/shell_parser/braces.zig#L388 https://github.com/oven-sh/bun/blob/bun-v1.3.14/src/shell_pa... The allocator via function parameter is only a convention and only even one convention. It is certainly a strong convention (throughout the standard library anyway) but it is just a convention. Recent Rust also has the ability to pass allocators but that's the same thing. And even if you use no_std in Rust you might call into some other library's no_std function that allocates and you wouldn't be able to grep for that.