Would you mind elaborating on the complications you ran into? I ask because I think you used std.HashMap instead of std.AutoHashMap, the latter of which automatically chooses a hash function based on the types provided.
Without getting into the weeds of why (happy to do so, just want to keep this readable), basically I needed to define and populate a hashmap in a new script and then import it into my main script, which to my mind left me with two options:
* Define and initialise it at the same time (my preferred method) as a constant. I don't have much to say on this as iirc, I had no luck with it at all, never even got close.
* Define it in a (public) function, add each field in with "put" and then return the hashmap. I tried with std.AutoHashMap and various other things, but to what I could work out there was no type of hashmap, so it wouldn't accept my return type.
fn buildMap(allocator: std.mem.Allocator) !std.AutoHashMap(u64, u64) {
var result = std.AutoHashMap(u64, u64).init(allocator);
errdefer result.deinit();
try result.put(10, 100);
try result.put(20, 200);
return result;
}
Your #1 option should be possible once Zig has comptime allocators -- it's on the roadmap, but not possible yet iirc.Cheers for letting me know though!
const mymap = {1: "hello", 2: "world"};
But the only thing I could find any answers for was something more like: const mymap; mymap.put(1, "hello"); mymap.put(2, "world");