../
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
importis being assigned to a constant.importtake an arg and finds that file and the items withpubare exported in the LHS- The LHS is just a
struct
- It has
.notation for functions - There is curios
pubkeyword - function is defined using
fn - The type is given after the function
- The second arg for
printhas this ugly.{}notation- This is an anonymous struct notation
printalways takes astructas it second arg.std.debug.print("Hello, {s}!\n", "World");won’t work- It has this
comptimetype that is assigned to constants
Pointers
[N]typeis an array. This is a data type. Just likeu8const foo: [69]u8 = [_]u8{0}**69initializes 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 = foomakes a copy. - This is just like
u8
[]typeis a slice. It is basically a fat pointer - pointer + len[*]typeis 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.
- This means that the address is starting of a contiguous block of
*[N]typeis a pointer to an array. This is equivalent to*typewhich is just a pointer to atype. But remember that[N]typeis also atype. So these are equivalent
stdio
- Everything has to go through the reader/writer interface in
std.Io.- Notice the capital
I std.iois old
- Notice the capital