Hi,
I'm new to zig and I was making a function that duplicates 2D arrays. But I found myself writing code that would be quite the same as if I was writing in C. I've spent a long time in C, so I'd like an exterior point of view that could tell me if I could achieve the same result in a way that would be more "Zig-like".
fn dupeMatrix(allocator: mem.Allocator, matrix: []const []const u8) mem.Allocator.Error![][]u8 {
var arr = try allocator.alloc([]u8, matrix.len);
var i: usize = 0;
while (i < matrix.len) : (i += 1)
arr[i] = allocator.dupe(u8, matrix[i]) catch |err| {
while (i > 0) : (i -= 1)
allocator.free(arr[i - 1]);
allocator.free(arr);
return (err);
};
return arr;
}
Thanks for reading this !