by dinglejungle · 5y ago · 0 comments
{
var file = try std.fs.cwd().openFile(path, .{});
defer file.close();
// ... code ...
// file.close() is executed here
}
would work just fine. try std.fmt.allocPrint(allocator, "problems/{}", .{ id }) |path| : allocator.free(path) {
try std.fs.cwd().openFile(path, .{}) |file| : file.close() {
try file.stat() |stat| {
try allocator.alloc(u8, stat.size) |contents| : allocator.free(contents) {
// do something with 'contents'
}
}
}
}
In Zig you can write this, and mess everything up: const path = try std.fmt.allocPrint(allocator, "problems/{}", .{ id });
var file = try std.fs.cwd().openFile(path, .{});
const stat = try file.stat();
var contents = try allocator.alloc(u8, stat.size);
defer allocator.free(contents);
defer file.close();
defer allocator.free(path);