zig 官网学习文档

创建日期: 2025-01-24 10:53 | 作者: 风波 | 浏览次数: 25 | 分类: zig

地址:https://ziglang.org/

const std = @import("std");
const parseInt = std.fmt.parseInt;

test "parse integers" {
    const input = "123 67 89,99";
    const ally = std.testing.allocator;

    var list = std.ArrayList(u32).init(ally);
    // Ensure the list is freed at scope exit.
    // Try commenting out this line!
    defer list.deinit();

    var it = std.mem.tokenizeAny(u8, input, " ,");
    while (it.next()) |num| {
        const n = try parseInt(u32, num, 10);
        try list.append(n);
    }

    const expected = [_]u32{ 123, 67, 89, 99 };

    for (expected, list.items) |exp, actual| {
        try std.testing.expectEqual(exp, actual);
    }
}

shell

$ zig test index.zig
1/1 index.test.parse integers...OK
All 1 tests passed.
25 浏览
15 爬虫
0 评论