ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward
introduce the core Zig syntax
explore the Zig build system
get a sense of how to use Zig well
"a general-purpose programming language and toolchain for maintaining robust, optimal and reusable software."
https://ziglang.org/
"A Simple Language: Focus on debugging your application rather than debugging your programming language knowledge.
No hidden control flow.
No hidden memory allocations.
No preprocessor, no macros."
https://ziglang.org/
"Comptime: A fresh approach to metaprogramming based on compile-time code execution and lazy evaluation.
Call any function at compile-time.
Manipulate types as values without runtime overhead.
Comptime emulates the target architecture."
https://ziglang.org/
package managers
download/manual install
build from source
macOS Homebrew: brew install zig
Windows:
Choco: choco install zig
Winget: winget install zig
Scoop: scoop install zig
Linux:
Ubuntu snap: snap install zig --classic --beta
FreeBSD: pkg install lang/zig
OpenSuSE: zypper install zig
https://ziglang.org/download/
Unzip to some global location
make sure to put the platform binary on the PATH
echo 'export PATH="$HOME/zig-linux-x86_64-0.12.0:$PATH"' >> ~/.bashrc
Validate/verify install
$ zig version 0.13.0
Hello World
const std = @import("std"); pub fn main() void { std.debug.print("Hello, {s}!\n", .{"World"}); }
zig run main.zig
zig init
in an empty directory
generates src/main.zig: executable
generates src/root.zig: library
generates tests for each (in each file)
zig build
: builds both library and executable
zig test
: executes tests for both
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()); }
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); }
create a file called "hello.c"...
#include <stdio.h> int main(int argc, char* argv[]) { printf("Hello, world!\n"); }
zig cc -o hello hello.c
create a file called "hello.cpp"...
#include <iostream> using namespace std; int main(int argc, char* argv[]) { cout << "Hello, world!" << endl; }
zig c++ -o hello hello.cpp
take the hello.c
file from earlier
zig translate-c hello.c > hello.zig
zig run hello.zig
(don't read the results--it's painful)
https://ziglang.org/
Main Zig website
Architect, Engineering Manager/Leader, "force multiplier"
http://www.newardassociates.com
http://blogs.newardassociates.com
Sr Distinguished Engineer, Capital One
Educative (http://educative.io) Author
Performance Management for Engineering Managers
Books
Developer Relations Activity Patterns (w/Woodruff, et al; APress, forthcoming)
Professional F# 2.0 (w/Erickson, et al; Wrox, 2010)
Effective Enterprise Java (Addison-Wesley, 2004)
SSCLI Essentials (w/Stutz, et al; OReilly, 2003)
Server-Based Java Programming (Manning, 2000)