I have this function to get a directory name, list all files/dirs and append them to a list:
fn appendEntries(allocator: std.mem.Allocator, list: *std.ArrayList(Entry), dir_name: []const u8) !void {
var dir = try fs.cwd().openIterableDir(dir_name, .{});
defer dir.close();
var it = dir.iterate();
while (try it.next()) |entry| {
if (entry.name[0] == '.') continue;
var dest = try allocator.alloc(u8, entry.name.len);
std.mem.copy(u8, dest, entry.name);
try list.append(.{ .name = dest, .kind = entry.kind });
}
}
Is there a way to do this without allocating additional memory for the names? An Entry is just a name: []const u8 and kind: fs.File.Kind. It made sense to me that since name points to some memory that I can't just magically keep it after the function returns and so I have to allocate somewhere, but I could be wrong.