|
|
|
|
|
by Validark
493 days ago
|
|
That's actually a great argument in favor of Zig over Rust. I assume Rust automatically writes code equivalent to this for you: ```
defer {
for (list.items) |str|
gpa.free(str);
list.deinit(gpa);
}
``` When it's spelled out like this, it becomes obvious to the reader that maybe this is the wrong allocation strategy. Maybe the whole thing should go in an Arena. Or, similarly, maybe there should be an ArrayList that holds all the character data that your string ArrayList indexes into with a u32 (or points to with pointers, if you want to update all the pointers on resize). Regardless, I'd be skeptical of code where each string has a separate lifetime even though all the lifetimes could be tied. Rust makes classic (bad) allocation strategies automatic. Zig makes good allocation strategies more attractive than classic (bad) allocation strategies. More succinctly: Rust makes bad code safe, Zig makes good code easy. |
|