[comment]: # "markdown: { smartypants: true }" ## Intro to Zig
(in a world that already has Go and Rust) [beyondcodebootcamp.github.io/presos/intro-to-zig](https://beyondcodebootcamp.github.io/presos/intro-to-zig/)
AJ ONeal
[@\_beyondcode](https://twitter.com/@_beyondcode)
[github.com/therootcompany](https://github.com/therootcompany/)
[twitch.tv/coolaj86](https://twitch.tv/coolaj86)
Dangerous Wrong Thinker Equal Opportunity Offender Technophobic Technologist Extraordinairé
## Intro to Zig [beyondcodebootcamp.github.io/presos/intro-to-zig](https://beyondcodebootcamp.github.io/presos/intro-to-zig/)
![](https://i.imgur.com/g8eOl9c.jpg)
## Guardrails, The Language ![](https://i.imgur.com/UJgoCT5.jpg)
> Java for people who don't hate themselves
### Go Pros - Learn in a Weekend - Batteries Included \ (std lib, tooling, etc) - Safe - Cohesive - Write Once, Run Anywhereâ„¢ - Fast...ish
### Go Cons - error handling - boilerplate - [cgo is not Go](https://dave.cheney.net/2016/01/18/cgo-is-not-go) - `go generate` ```go // no hidden control flow! n, err := r.Read(buf) if err != nil { return nil, err } ```
## Cool Kids Kool Aid
> C++ for people who care about their sanity
### Rust Pros - Perfectly Safe - As fast as C - Community - Tooling
### Rust Cons - Learning Curve - _Dependent_ on C - Macros - Drama ```text error[E0502]: cannot borrow `vec` as mutable because it is also borrowed as immutable --> src/main.rs:5:5 | 4 | let first = &vec[0]; | ---- immutable borrow occurs here 5 | vec.push(4); | ^^^ mutable borrow occurs here ```
## We Were Promised Jet-Packs ![](https://i.imgur.com/lljGPNf.png)
> C for people who want software the works
### Zig Pros
(Cross) Compile C (and `cgo`) and Rust \
(and Zig, of course!)
```sh CGO_ENABLED=1 CC="zig cc" CXX="zig cc" go test -c ```
andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html
Learn in a Week -
-
-
Easier to do right than wrong ```zig const chars = try reader.readBytesNoEof(6); // vs const chars = reader.readBytesNoEof(6) catch |err| { ... }; ``` ```zig var elems = [_][]const u8 { "water", "earth", "fire", "air" }; var nats = [_][]const u8 { "tribes", "kingdom", "nation", "nomads" }; for (elems, nats, 0..) |e, n, idx| { std.debug.print("{} - {s} {s}\n", .{idx, e, n}); } ```
Arenas over Ownership (or GC) ```zig var allocator = if (builtin.mode == .Debug) blk: { break :blk gpa.allocator(); } else blk: { break :blk std.heap.page_allocator; }; ```
No Macros, Just `comptime` ```zig fn multiply(a: i64, b: i64) i64 { return a * b; } pub fn main() void { const len = comptime multiply(4, 5); const my_static_array: [len]u8 = undefined; } ```
"Dynamic" Types & Generics ```zig fn pipe(reader: anytype, writer: anytype) !void { const bytes = try reader.readBytesNoEof(6); try writer.writeAll(&bytes); // ... } ```
The Philosophy
https://youtube.com/watch?v=Gv2I7qTux7g
The Zen of Zig ![](https://i.imgur.com/KTEzl3F.png)
### Zig Cons - Maturity - Autonomous Collective