Skip to content

Zig

Zig

Using Zig 0.11 in 2023

Zig has a package manager

As of this writing, October 6 2023, the package manager is currently being worked on for the next release: 0.12.

Here are some relevant Github links detailing that work:

”To use a package that’s been prepared for the new Zig package manager, you just need to list it in the dependencies section.”

Zon. It uses files such as build.zig.zon which contains a zig anonymous struct. Example:

1
// because zon file is really just a zig struct
2
// comments are really done in the same way using 2 forward slashes
3
.{
4
// the name of your project
5
.name = "my-wtf-project",
6
// the version of your project
7
.version = "0.0.1",
8
9
// the actual packages you need as dependencies of your project
10
.dependencies = .{
11
// the name of the package
12
.zap = .{
13
// the url to the release of the module
14
.url = "https://github.com/zigzap/zap/archive/refs/tags/v0.1.7-pre.tar.gz",
15
// the hash of the module, this is not the checksum of the tarball
16
.hash = "1220002d24d73672fe8b1e39717c0671598acc8ec27b8af2e1caf623a4fd0ce0d1bd",
17
},
18
}
19
}
  1. run zig init-exe
  2. edit files
  3. zig build
  4. See dir zig-out/bin

Zig concurrency

TODO

Zig Memory alocation

TODO

For loops

For loops recently changed in Zig

1
const std = @import("std");
2
3
pub fn main() void {
4
const name = "Josey";
5
for (name, 0..) |char, i| {
6
_ = char;
7
std.debug.print("{c}\n", .{name[i]});
8
}
9
10
var elems = [_][]const u8{ "water", "earth", "fire", "air" };
11
var nats = [_][]const u8{ "tribes", "kingdom", "nation", "nomads" };
12
13
for (elems, nats) |e, n| {
14
std.debug.print("{s} {s}\n", .{ e, n });
15
}
16
}