Basic Zig Project
Now that you have got zig up and running, it's time to start a basic project in zig.
-
Create a new directory where you want to put the new project.
mkdir my-zig-project && cd my-zig-project -
Initialize a zig project in the current directory
zig init-exeIf you see something like
info: Created build.zig info: Created src\main.zig info: Next, try `zig build --help` or `zig build run`then you have successfuly created a zig project. If not, then go through the installation section to check if you made any mistakes.
-
Open the folder in a text editor.
You should see a folder calledsrc. This is where source code of your project lives.const std = @import("std"); pub fn main() !void { // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); // stdout is for the actual output of your application, for example if you // are implementing gzip, then only the compressed bytes should be sent to // stdout, not any debugging messages. const stdout_file = std.io.getStdOut().writer(); var bw = std.io.bufferedWriter(stdout_file); const stdout = bw.writer(); try stdout.print("Run `zig build test` to run the tests.\n", .{}); try bw.flush(); // don't forget to flush! } test "simple test" { var list = std.ArrayList(i32).init(std.testing.allocator); defer list.deinit(); // try commenting this out and see if zig detects the memory leak! try list.append(42); try std.testing.expectEqual(@as(i32, 42), list.pop()); }For now, remove everything from
test "simple test" {Now, lets go step by step throught the file.
main.zig
-
The first line in
main.zigis#![allow(unused)] fn main() { const std = @import("std"); }constis used to declare constant namedstd.
@import("std")tells the zig compiler that we want to import the The Zig Standard Library is needed for things like printing to stdout, opening files, networking etc. -
pub fn main() void {}is used to declare a function.
Thefnkeyword declares a function and main is the name of the function.
The return type comes aftermain(). Here, the return type isvoid.voidbasicaly means we don't have anything to return.
mainis a special function that is excuted when the executable is run. It is the entry point of the executable.