../

Zig

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, {s}!\n", .{"World"});
}

This is a hello, world program in zig.

Initial reactions

  • The import is being assigned to a constant.
    • import take an arg and finds that file and the items with pub are exported in the LHS
    • The LHS is just a struct
  • It has . notation for functions
  • There is curios pub keyword
  • function is defined using fn
  • The type is given after the function
  • The second arg for print has this ugly .{} notation
    • This is an anonymous struct notation
    • print always takes a struct as it second arg.
    • std.debug.print("Hello, {s}!\n", "World"); won’t work
    • It has this comptime type that is assigned to constants

Pointers

  • [N]type is an array. This is a data type. Just like u8
    • const foo: [69]u8 = [_]u8{0}**69 initializes an empty array
    • It is a data type. Not like C where they can be decomposed to pointers
    • That means that const foo: [5]u8; const bar = foo makes a copy.
    • This is just like u8
  • []type is a slice. It is basically a fat pointer - pointer + len
  • [*]type is many item pointers. This is closest equivalent to a C pointer.
    • This means that the address is starting of a contiguous block of type
    • This doesn’t have the length.
    • You will never encounter them if you stay within zig
    • But the length is inferred in some other way. May be you are interfacing with a C api and they usually specify the length.
  • *[N]type is a pointer to an array. This is equivalent to *type which is just a pointer to a type. But remember that [N]type is also a type. So these are equivalent

stdio

  • Everything has to go through the reader/writer interface in std.Io.
    • Notice the capital I
    • std.io is old