如何释放 StringHashMap 的键?
How to free keys of StringHashMap?
我在尝试
test "foo" {
var map = std.StringHashMap(void).init(std.testing.allocator);
defer {
while (map.keyIterator().next()) |key| {
std.testing.allocator.free(key);
}
map.deinit();
}
}
但是遇到编译错误
/snap/zig/4365/lib/std/mem.zig:2749:9: error: expected []T or *[_]T, passed *[]const u8
@compileError("expected []T or *[_]T, passed " ++ @typeName(sliceType));
^
/snap/zig/4365/lib/std/mem.zig:2756:59: note: called from here
pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
^
/snap/zig/4365/lib/std/mem.zig:2756:59: note: called from here
pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
^
./main.zig:169:39: note: called from here
std.testing.allocator.free(key);
^
./main.zig:165:12: note: called from here
test "foo" {
不胜感激!如果你能分享一下如果你遇到同样的情况,你会在搜索引擎中搜索什么,或者在 zig std 代码库中找到什么来找出解决方案,那也太好了!因为我仍然很难自己找出解决方案。谢谢!
Key 是指向此错误所说的键的指针
error: expected []T or *[_]T, passed *[]const u8
要从中获取 []const u8
,您必须取消引用它 (key.*
)
test "foo" {
var map = std.StringHashMap(void).init(std.testing.allocator);
defer {
while (map.keyIterator().next()) |key| {
std.testing.allocator.free(key.*);
}
map.deinit();
}
}
我在尝试
test "foo" {
var map = std.StringHashMap(void).init(std.testing.allocator);
defer {
while (map.keyIterator().next()) |key| {
std.testing.allocator.free(key);
}
map.deinit();
}
}
但是遇到编译错误
/snap/zig/4365/lib/std/mem.zig:2749:9: error: expected []T or *[_]T, passed *[]const u8
@compileError("expected []T or *[_]T, passed " ++ @typeName(sliceType));
^
/snap/zig/4365/lib/std/mem.zig:2756:59: note: called from here
pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
^
/snap/zig/4365/lib/std/mem.zig:2756:59: note: called from here
pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
^
./main.zig:169:39: note: called from here
std.testing.allocator.free(key);
^
./main.zig:165:12: note: called from here
test "foo" {
不胜感激!如果你能分享一下如果你遇到同样的情况,你会在搜索引擎中搜索什么,或者在 zig std 代码库中找到什么来找出解决方案,那也太好了!因为我仍然很难自己找出解决方案。谢谢!
Key 是指向此错误所说的键的指针
error: expected []T or *[_]T, passed *[]const u8
要从中获取 []const u8
,您必须取消引用它 (key.*
)
test "foo" {
var map = std.StringHashMap(void).init(std.testing.allocator);
defer {
while (map.keyIterator().next()) |key| {
std.testing.allocator.free(key.*);
}
map.deinit();
}
}