创建一个带有字符串参数的结构体

发布于 2025-01-10 19:00:49 字数 316 浏览 0 评论 0 原文

我只想创建一个带有变量字符串(utf-8 文本)的结构。

const Person = struct {
    name: [_]u8, 

};

是否可以?或者我必须设置字符串的最大长度(例如 name: [255]u8;)?当我传递给编译器时,它说:

person.zig:5:12: error: unable to infer array size
    name: [_]u8,

无论如何,我想念本机字符串类型,而不是必须处理字节。有相关的图书馆吗?

I just want to create a struct with variable String (utf-8 text).

const Person = struct {
    name: [_]u8, 

};

Is it possible? Or I have to set maximum length of string (e.g. name: [255]u8;)? When I pass to compiler it says:

person.zig:5:12: error: unable to infer array size
    name: [_]u8,

Anyway I miss native String type instead of having to handle with bytes. Is there any library for that?

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

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

发布评论

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

评论(1

泪是无色的血 2025-01-17 19:00:49

您可能正在寻找切片类型:[]u8[]const u8。切片类型包含一个指针和一个长度,因此该结构实际​​上并不直接保存字符串的内存,而是保存在其他地方。 https://ziglang.org/documentation/0.9.1/#Slices

const Person = struct {
    name: []const u8,
};

无论如何,我怀念原生的 String 类型,而不是必须处理字节。有没有相关的库?

有一些用于 zig 的字符串库,但这取决于您具体要寻找的功能。如果您正在寻找字符串连接和格式化,您可能可以使用zig的内置ArrayList

const std = @import("std");

const Person = struct {
    name: std.ArrayList(u8),
};

test "person" {
    const allocator = std.testing.allocator;

    var person: Person = .{
        .name = std.ArrayList(u8).init(allocator),
    };
    defer person.name.deinit();
    try person.name.appendSlice("First ");
    try person.name.appendSlice("Last");
    try person.name.writer().print(". Formatted string: {s}", .{"demo"});

    try std.testing.expectEqualSlices(u8, "First Last. Formatted string: demo", person.name.items);
}

如果您正在寻找诸如字符串规范化之类的unicode函数,您可以使用像之字形

You might be looking for a slice type: []u8 or []const u8. A slice type contains a pointer and a length, so the struct does not actually directly hold the string's memory, it is held somewhere else. https://ziglang.org/documentation/0.9.1/#Slices

const Person = struct {
    name: []const u8,
};

Anyway I miss native String type instead of having to handle with bytes. Is there any library for that?

There are some string libraries for zig, but this depends on what features specifically you are looking for. If you are looking for string concatenation and formatting, you can probably use zig's builtin ArrayList

const std = @import("std");

const Person = struct {
    name: std.ArrayList(u8),
};

test "person" {
    const allocator = std.testing.allocator;

    var person: Person = .{
        .name = std.ArrayList(u8).init(allocator),
    };
    defer person.name.deinit();
    try person.name.appendSlice("First ");
    try person.name.appendSlice("Last");
    try person.name.writer().print(". Formatted string: {s}", .{"demo"});

    try std.testing.expectEqualSlices(u8, "First Last. Formatted string: demo", person.name.items);
}

If you are looking for unicode functions like string normalization, you can use a library like Ziglyph

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