编译Zig中的时间哈希图

发布于 2025-02-12 12:15:10 字数 915 浏览 2 评论 0 原文

以Rust库 lazy_static 's example

use lazy_static::lazy_static;
use std::collections::HashMap;

lazy_static! {
    static ref HASHMAP: HashMap<u32, &'static str> = {
        let mut m = HashMap::new();
        m.insert(0, "foo");
        m.insert(1, "bar");
        m.insert(2, "baz");
        m
    };
    static ref COUNT: usize = HASHMAP.len();
    static ref NUMBER: u32 = times_two(21);
}

how how how how how这可以在Zig中完成吗?

我已经尝试了这一点,这对我来说是唯一有意义的事情:

const std = @import("std");

pub fn main() void {
    comptime var h = std.StringHashMap(i32).init(std.testing.allocator);
    h.put("hi", 5) catch {};
    std.debug.print("{}", .{h});
}

但是这是segfault。

甚至可以在Zig中这样做吗?

Take for example the Rust library lazy_static's example:

use lazy_static::lazy_static;
use std::collections::HashMap;

lazy_static! {
    static ref HASHMAP: HashMap<u32, &'static str> = {
        let mut m = HashMap::new();
        m.insert(0, "foo");
        m.insert(1, "bar");
        m.insert(2, "baz");
        m
    };
    static ref COUNT: usize = HASHMAP.len();
    static ref NUMBER: u32 = times_two(21);
}

How might this be done in Zig?

I have tried this which is the only thing that makes sense to me:

const std = @import("std");

pub fn main() void {
    comptime var h = std.StringHashMap(i32).init(std.testing.allocator);
    h.put("hi", 5) catch {};
    std.debug.print("{}", .{h});
}

but this segfaults.

Is it even possible to do this in Zig?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔日梦未散 2025-02-19 12:15:10

由于Zig 0.13.0

具有Zig 0.13.0,因此以前已知的 std.comptimestringmap /commit/8AF59D1F98266BD70B3AFB44D196BBD151CEDF22“ rel =“ nofollow noreferrer”>提交8af59d1 std.staticstringmap ,现在活着在这里图书馆

直到Zig 0.12.1

似乎(感谢此 reddit post )这是在标准库

,可以通过 std使用。 CompTimestringMap 这似乎不支持任何动态插入。

但是,由于没有 insert 方法,因此

Since zig 0.13.0

With zig 0.13.0, the previously known std.ComptimeStringMap was renamed in commit 8af59d1 to std.StaticStringMap, and now lives here in the standard library.

Until zig 0.12.1

It seems (thanks to this Reddit post) that this is implemented in the standard library

and can be used via std.ComptimeStringMap

This however does not seem to support any dynamic insertion as there is no insert method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文