github.com/jiacai2050
2024-01-20
Zig 是一种通用编程语言和工具链,用于维护稳健、优化和可重复使用的软件
Better C
没有隐藏的控制流
var a = b + c.d;
foo();
bar();
基于编译时代码执行和懒惰评估的元编程新方法
zig build
在所有平台上创建一致的开发环境const std = @import("std");
pub fn main() void {
std.debug.print("Hello World", .{});
}
const file = try fs.cwd().openFile("test.txt", .{});
defer file.close();
const reader = file.reader();
var line = std.ArrayList(u8).init(allocator);
defer line.deinit();
const writer = line.writer();
var line_no: usize = 1;
while (reader.streamUntilDelimiter(writer, '\n', null)) : (line_no += 1) {
defer line.clearRetainingCapacity();
print("{d}--{s}\n", .{ line_no, line.items });
} else |err| switch (err) {
error.EndOfStream => {}, // Continue on
else => |e| return e, // Propagate error
}
来源:Zig cookbook
fn LinkedList(comptime T: type) type {
return struct {
pub const Node = struct {
prev: ?*Node,
next: ?*Node,
data: T,
};
first: ?*Node,
last: ?*Node,
len: usize,
};
}
const ListOfInts = LinkedList(i32);
try expect(ListOfInts == LinkedList(i32));
var node = ListOfInts.Node {
.prev = null,
.next = null,
.data = 1234,
};
var list2 = LinkedList(i32) {
.first = &node,
.last = &node,
.len = 1,
};
const ray = @cImport({
@cInclude("raylib.h");
});
pub fn main() void {
const screenWidth = 800;
const screenHeight = 450;
ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
defer ray.CloseWindow();
ray.SetTargetFPS(60);
while (!ray.WindowShouldClose()) {
ray.BeginDrawing();
defer ray.EndDrawing();
ray.ClearBackground(ray.RAYWHITE);
ray.DrawText("Hello, World!", 190, 200, 20, ray.LIGHTGRAY);
}
}
$ zig build-exe main.zig -lc $(pkg-config --cflags --libs raylib)
$ ./main