Busy Developer's Guide to Zig

ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward

Objectives

What do we want to do today?

Overview

What is this thing?

Overview

Zig is....

"a general-purpose programming language and toolchain for maintaining robust, optimal and reusable software."

Overview

Zig is....

"A Simple Language: Focus on debugging your application rather than debugging your programming language knowledge.

Overview

Zig is...

"Comptime: A fresh approach to metaprogramming based on compile-time code execution and lazy evaluation.

Getting Started

From 0 to Hello World in 3... 2... 1...

Getting Started

Installation

Getting Started

Package Managers

Getting Started

Download/Manual install

Getting Started

Validate/verify install

$ zig version
0.13.0

Getting Started

Hello World

const std = @import("std");

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

Getting Started

Slightly more complicated version

Getting Started

src/main.zig

const std = @import("std");

pub fn main() !void {
    std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

    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());
}

Getting Started

src/root.zig

const std = @import("std");
const testing = std.testing;

export fn add(a: i32, b: i32) i32 {
    return a + b;
}

test "basic add functionality" {
    try testing.expect(add(3, 7) == 10);
}

Getting Started

Another version

Getting Started

#include <stdio.h>

int main(int argc, char* argv[]) {
    printf("Hello, world!\n");
}

zig cc -o hello hello.c

Getting Started

Yet another version

Getting Started

#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    cout << "Hello, world!" << endl;
}

zig c++ -o hello hello.cpp

Getting Started

For even more fun....

Resources

Where to go to get more

Resources

Web

Summary

So... now what?

Credentials

Who is this guy?