You don't have to know Zig to follow along. I'll tell you what you need. You should have touched Python or something similar before.
Go to ziglang.org and download the latest stable release. Extract it, put the folder in your PATH.
zig version
If you get a version number, you're good.
Create a folder for your project, cd into it, make a file minizig.zig:
const std = @import("std");
const print = std.debug.print;
pub fn main() void {
print("hello, world\n", .{});
}
Compile:
zig build-exe minizig.zig
This produces minizig.exe on Windows, minizig elsewhere. Run it:
./minizig.exe
You should see hello, world. That's it. You have a working Zig setup.
Now try something. In main, declare two values:
const some_constant_number: i64 = 34;
var some_variable_number: i64 = 35;
First, try to modify the const:
some_constant_number = 99;
Compile. What does Zig say?
Then remove that line and try the other direction: leave the var alone. Don't assign anything to it after its declaration. Compile. What does Zig say now?
Finally, print both values using print. The second argument to print is a tuple of values, written .{ a, b }. Placeholders: {d} for an integer.
const std = @import("std");
const print = std.debug.print;
pub fn main() void {
print("hello, world\n", .{});
const some_constant_number: i64 = 34;
// some_constant_number = 99; // error: cannot assign to constant
var some_variable_number: i64 = 35;
some_variable_number = 42; // a var that never changes is an error too.
// print's second arg is a tuple of values: .{ a, b, c }.
// {d} for a decimal integer, {s} for a string, {c} for a char.
print("const = {d}, var = {d}\n", .{ some_constant_number, some_variable_number });
}
Some people don't like being forced to modify their variables, but I like it. The ZLS extension transforms var into const and conversely according to what you do with it, so you don't have to think about it. It's all about reading being more important than writing.
When I see a var, I have to reserve mental room for whatever might happen to it later. If a var never actually gets reassigned, I've wasted that room, which annoys me.