I'm learning Zig and
pub fn foo() usize {
var gpa = std.heap.GeneralPurposeAllocator(.{ .verbose_log = true }){};
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();
var map = std.StringHashMap(i32).init(allocator);
try map.put("example", 0);
}
When I try to compile and run this code, I get the following error:
solution.zig:11:5: error: expected type 'usize', found 'error{OutOfMemory}' try map.put("example", 0); ^~~~~~~~~~~~~~~~~~~~~~~~~
I'm not sure what I'm doing wrong. I am using Zig version 0.13.0
tryin a function requires the return type to be an error union, i.e. error or value (cmp. Result in Rust). In addition, note thatmap.puthas return type "error or void" (src), so unless you plan to return some other parameter (usize) from your function, the return type of foo would be!voidin your example.