菜鸟问题:
我想改变数组列表中存在的值。我最初尝试获取索引项并直接更改其字段值。
const Foo = struct {
const Self = @This();
foo: u8,
};
pub fn main() anyerror!void {
const foo = Foo {
.foo = 1,
};
const allocator = std.heap.page_allocator;
var arr = ArrayList(Foo).init(allocator);
arr.append(foo) catch unreachable;
var a = arr.items[0];
std.debug.warn("a: {}", .{a});
a.foo = 2;
std.debug.warn("a: {}", .{a});
std.debug.warn("arr.items[0]: {}", .{arr.items[0]});
//In order to update the memory in [0] I have to reassign it to a.
//arr.items[0] = a;
}
Run Code Online (Sandbox Code Playgroud)
然而结果却出乎我的意料:
a: Foo{ .foo = 1 …Run Code Online (Sandbox Code Playgroud) 我正在慢慢学习 zig,但我不了解 const 以及它如何与数组/类型交互 - 我正在浏览https://ziglang.org/documentation/0.6.0/#Introduction 但他们经常使用 const字符串。
这编译:
var n = [_][]const u8 {"test 1", "test4", "test 6", "zz"};
Run Code Online (Sandbox Code Playgroud)
没有const是一个错误:
var n = [_][] u8 {"test 1", "test4", "test 6", "zz"};
error: expected type '[]u8', found '*const [6:0]u8'
Run Code Online (Sandbox Code Playgroud)
同样,将 const 放在左侧也是同样的错误:
const n = [_][]u8 {"test 1", "test4", "test 6", "zz"};
Run Code Online (Sandbox Code Playgroud)
是什么把 const 关键字放在中间,这样实际上指导编译器做什么?
在 zig 中,可以使用“{d}”以十进制表示法打印浮点值。这将自动以全精度打印该值。有没有办法指定位数?要么针对每个值,要么作为某种全局设置?
语境
对这门语言非常陌生,所以请耐心等待。我正在编写一个超级基本函数来打印传递给程序的命令行参数。这是关键逻辑:
// already created allocator (std.heap.ArenaAllocator) and iterator (std.process.ArgIterator)
var idx: u16 = 0;
while (true) {
var arg = iterator.next(&allocator.allocator) catch |err| {
// ...
};
if (arg == null) {
print("End of arguments, exiting.", .{});
break;
}
print("Argument {d}: {s}", .{idx, arg});
idx += 1;
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一条错误消息:
error: expected error union type, found '?std.process.NextError![:0]u8'
var arg = iterator.next(&allocator.allocator) catch |err| return err;
Run Code Online (Sandbox Code Playgroud)
NextError我认为这个问题与返回可选错误联合的事实有关。但我不能确定,因为我还没有找到任何涵盖此特定案例的文档。
问题
我通过删除捕获并假装返回类型的错误部分不存在来使此代码正常工作。但问题是,捕获该错误的正确方法是什么?
当我有一个[*]u8指针和一个usize长度时,如何将指针转换为[]u8指定长度的切片?
我只想创建一个带有变量字符串(utf-8 文本)的结构。
const Person = struct {
name: [_]u8,
};
Run Code Online (Sandbox Code Playgroud)
是否可以?或者我必须设置字符串的最大长度(例如name: [255]u8;)?当我传递给编译器时,它说:
person.zig:5:12: error: unable to infer array size
name: [_]u8,
Run Code Online (Sandbox Code Playgroud)
无论如何,我怀念本机字符串类型,而不是必须处理字节。有相关的图书馆吗?
Zig 字符串文字是指向以 null 结尾的字节数组的单项指针,非常适合用作 C 的char *字符串!
然而,当我尝试使用 Zig 中的这个简单的 C 函数时:
int count_bytes(const char *str) {
int count = 0;
while(str[count]) {
count++;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
齐格呼叫者:
const std = @import("std");
const c = @cImport({
@cInclude("add.c");
});
const testing = std.testing;
test "should be able to count string length" {
try testing.expectEqual(0, c.count_bytes(""));
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
./src/main.zig:16:46: error: expected type '[*c]u8', found '*const [0:0]u8'
try testing.expectEqual(0, c.count_bytes(""));
^
./src/main.zig:16:46: note: cast discards const qualifier
try …Run Code Online (Sandbox Code Playgroud) 有没有办法访问std.fs.Dir结构体的完整路径?我已经查看了源代码中的所有方法,但找不到任何获取目录上路径相关信息的内容。
当我使用函数指针时,我发现 Zig 的一个有点奇怪的特性。
这是函数指针类型的一个简单示例:
const std = @import("std");
const print = std.debug.print;
const aFunc = fn (x: i32) void;
fn theFunc(x: i32) void {
print("have {}\n", .{x});
}
pub fn main() void {
const f: aFunc = theFunc;
f(3);
}
Run Code Online (Sandbox Code Playgroud)
这段代码编译并运行正常。
现在像这样更改它,向aFunc类型定义添加一个名称:
const std = @import("std");
const print = std.debug.print;
const aFunc = fn someFunc (x: i32) void;
fn theFunc(x: i32) void {
print("have {}\n", .{x});
}
pub fn main() void {
const f: aFunc = theFunc;
f(3); …Run Code Online (Sandbox Code Playgroud) 我在字符串中有一个简单的句子。我想在新行上打印每个单词,或者只是对每个单词进行一些计算?
zig中有类似python的东西吗"Hello World".split()?像这样的东西:
var arr = std.strings.split("Hello world");
Run Code Online (Sandbox Code Playgroud)