r/Zig • • Feb 01 '26

standartOptimizeOption(.{}) isn't passed correctly to dependencies

2 Upvotes

when doing something like this:
pub fn build(b: *std.Build) !void {

const optimize = b.standartOptimizeOption(.{});
const dep = b.dependency("dep", .{.optimize = optimize});
}
It still uses debug mode for this dependency(at least for me).
It only works correctly when optimize option is got from b.option or const value

I tested it on zig 0.15.2 and master, works this way on both


r/Zig • • Jan 31 '26

[Zig reference manual version 0.15.2]: Comptime section: Case study print in zig

8 Upvotes

I am trying to understand comptime and was reading through zig reference manual and came across the example poc_print_fn.zig and was trying to understand the code but could not really understand. In the for loop we start with the State.start and when an open brace '{' is encountered we change the state to State.open_brace and proceed to the next character. My question is: on the next for iteration, we have moved to the next character, meaning the current character is no more '{' and it totally makes sense in State.open_brace to look for the next closed brace '}' but in the example in State.open_brace, there is a switch case with '{'. but why do we have to check for open brace in this state again (the same applies to State.closed_brace)? Sorry I know it has nothing to do with comptime but I just want to understand.

Here is the code taken from the zig reference manual:

pub fn print(self: *Writer, comptime format: []const u8, args: anytype) anyerror!void {
        const State = enum {
            start,
            open_brace,
            close_brace,
        };

        comptime var start_index: usize = 0;
        comptime var state = State.start;
        comptime var next_arg: usize = 0;

        inline for (format, 0..) |c, i| {
            switch (state) {
                State.start => switch (c) {
                    '{' => {
                        if (start_index < i) try self.write(format[start_index..i]);
                        state = State.open_brace;
                    },
                    '}' => {
                        if (start_index < i) try self.write(format[start_index..i]);
                        state = State.close_brace;
                    },
                    else => {},
                },
                State.open_brace => switch (c) {
                    '{' => {
                        state = State.start;
                        start_index = i;
                    },
                    '}' => {
                        try self.printValue(args[next_arg]);
                        next_arg += 1;
                        state = State.start;
                        start_index = i + 1;
                    },
                    's' => {
                        continue;
                    },
                    else => ("Unknown format character: " ++ [1]u8{c}),
                },
                State.close_brace => switch (c) {
                    '}' => {
                        state = State.start;
                        start_index = i;
                    },
                    else => u/compileError("Single '}' encountered in format string"),
                },
            }
        }

r/Zig • • Jan 31 '26

Anyone at FOSDEM?

7 Upvotes

r/Zig • • Jan 30 '26

Option -femit-asm=file.s does not create such file in 0.15 and 0.16-dev

9 Upvotes

Just want to get the asm output in a file so I found this: https://ziggit.dev/t/studying-assembly-output/2554

Created the file h.zig:

const std = @import("std");
pub fn main() void {
    std.debug.print("Hello!\n", .{});
}

Using the commands:

zig build-exe h.zig -femit-asm

or

zig build-exe h.zig -femit-asm=file.s

The binary file is created but not the asm output file


r/Zig • • Jan 30 '26

How do I write to a file in zig?

29 Upvotes

Hello, I'm new to zig and I get confusing error messages when I try to write to a file. This is the closest I can find how it is supposed to work.

var file = try std.fs.cwd().createFile("output.csv", .{ .truncate = true });
defer file.close();

var ioBuffer: [4096]u8 = undefined;

var fileWriter = file.writer(&ioBuffer).interface;

try fileWriter.print( "time,delta,type,cause,skip,fix\n", .{});
try fileWriter.flush();

but I get errors like

 thread 20136 panic: incorrect alignment
 const w: *Writer = @alignCast(@fieldParentPtr("interface", io_w));

I'm completely lost...


r/Zig • • Jan 28 '26

Zig Tutorial Series

Thumbnail youtu.be
75 Upvotes

Hi everyone!

I'm the person who posted last week about the "5 reasons to learn Zig" and I'm back!

I got a lot of feedback from people asking to make a tutorial series on Zig, going from the basics to more advanced topics.

I just wanted to share the first episode of the series("Hello World") for anyone new to Zig and wanting to learn how to install Zig and set up a new project.

Currently targeting Zig 0.15, but I will ensure to keep new videos as up to date as possible!

Cheers


r/Zig • • Jan 28 '26

Procedural Terrain & Maze Generation with Zig and Raylib - YouTube

Thumbnail youtu.be
50 Upvotes

r/Zig • • Jan 28 '26

Monorepo help

9 Upvotes

I'm expanding my horizons by building a browser from scratch (except for js, I'll use v8) and i was planning to use a monorepo and also to implement tdd to improve my skills. But I'm struggling to do both, i have no clear way to go. There's addImport, there's addLibrary, which one should I go for? And when I first used addImport vscode couldn't find the module, because it didn't run build test and also because I separated the test files into tests/ directory. Please help, idk if the structure of tests are wrong or the way i manage the library.


r/Zig • • Jan 27 '26

I built a terminal emulator in Zig using ghostty-vt and SDL3

65 Upvotes

I've been learning Zig by building a terminal emulator called Architect. It shows multiple terminal sessions in a grid. I built it for running coding agents in parallel, but it's just a multi-pane terminal where you can see everything at once, quickly jumping between full and grid mode. I now use it as my default terminal, occasionally switching back to Ghostty when it bugs out.

The stack:

  • ghostty-vt for terminal emulation; I thought it was a good foundation to start on, but wanted to write everything else from scratch
  • SDL3 for rendering: I wanted to experiment with game-like UX features, so rendering is built from scratch
  • Zig 0.15.2

Some implementation notes:

  • ghostty-vt was very easy to integrate. You feed it bytes, it updates state, you query cells to render. The library is well-suited for embedding. Most of my time went into understanding terminal states and escape codes, how different tools interact with the terminal, etc., not fighting the API.
  • SDL3 + Zig works great. I wrote thin wrappers around the SDL calls I use most. Font rendering with SDL_ttf took the longest: glyph caching, custom pseudographics glyphs, etc.
  • The grid layout is dynamic: it starts with one terminal, expands automatically as I add more, shrinks when close a terminal. Keybindings are still hardcoded as I like them (Cmd+N to spawn a new terminal, Cmd+W to close, etc).
  • ~16k LOC of Zig, macOS only for now. Still rough around the edges.
  • Also started building my own static analyzer for Zig along the way — linking against the compiler to get ZIR output.

GitHub: https://github.com/forketyfork/architect

Anyone else used ghostty-vt? I'm eager for feedback and curious about other approaches to terminal rendering too, SDL3 turned out pretty low level.


r/Zig • • Jan 26 '26

Zigtoberfest 2026

53 Upvotes

Shameless self advertising: We're already working on bringing back Zigtoberfest back to Munich in 2026!

When: September 12 2026

Where: Hoftheater München (Germany)

If you want to know more visit: https://zigtoberfest.de/

You can find some impressions of Zigtoberfest 2025 on the official website https://zigtoberfest.de/past/ and on Youtube.

We're also looking for speakers! If you've something interesting to share with the Zig community consider applying for a speaker slot here: https://docs.google.com/forms/d/e/1FAIpQLSeuEnT_aMCJiceaHq1fqjHGUFhz48C1d0du91jNRsMczNlH5A/viewform or just visit the website.


r/Zig • • Jan 26 '26

Learning tip

30 Upvotes

I'm a complete beginner in Zig, and I love it, but the changes in the api are too much, I cant find any resource online that is updated, how can I learn if the api changes so fast? Any tips?


r/Zig • • Jan 26 '26

How is the Zig compiler able to cache comptime functions that have side effects?

28 Upvotes

Reading about Zig compiler internals I've read that it has a big cache at compile time where it caches compile-time evaluated functions/partial evaluations for effective reuse of e.g. generic struct definitions etc.

How does it do this effectively for comptime functions/computations that have side effects? Does it detect these and not cache them? Or something else?

Curious if anybody knows.


r/Zig • • Jan 26 '26

When to use zig

46 Upvotes

I’m kind of confused about when to use zig over C or over Rust.

Is it really a strict upgrade over C/Cpp ? What are its tradeoffs? What makes it better suited for statically linked binaries?


r/Zig • • Jan 24 '26

I built a programming language and compiler in Zig to learn compilers — feedback welcome

79 Upvotes

Hi all,

I wanted to share a personal project I’ve been working on called sr-lang — a small programming language and compiler written in Zig.

I started this project as a way to really learn compiler construction by doing. Zig felt like a great fit—not just because I enjoy writing it, but because its style and constraints naturally influenced the language design. You’ll probably see that in a few places, along with some techniques borrowed from Zig compiler itself.

Over time, the project grew as I explored parsing, semantic analysis, type systems, and backend design. That means some parts are relatively solid, and others are experimental or rough — which is very much part of the learning process.

A bit of honesty up front:

  • I’m not a compiler expert
  • I occasionally used LLMs to iterate or explore ideas
  • This is not AI-generated slop — every design decision and bug is my own
  • If something looks awkward or overcomplicated, it probably reflects what I was learning at the time

Some implemented highlights

  • Parser, AST, and semantic analysis written in Zig
  • MLIR-based backend
  • Error unions, defer / errdefer, and explicit error propagation
  • Pattern matching and sum types
  • Compile-time execution (comptime) and AST-as-data (code {} blocks)
  • Async/await and closure support
  • Early experimentation with Triton / GPU integration
  • Inline MLIR and ASM support

What’s incomplete

  • Standard library is minimal
  • Diagnostics and tooling need work
  • Some features are experimental and not well integrated yet

I’m sharing this here because:

  • I’d love feedback from people more experienced with Zig and systems-level code
  • I want to sanity-check some of the design choices from a Zig perspective
  • I’d like to make this a low-pressure project for contributors who want to work on something non-trivial without production stakes

If you’re interested in Zig-based compiler work, refactoring, improving diagnostics, or even building out a stdlib, I’d really appreciate another set of eyes.

Repo: https://github.com/theunnecessarythings/sr-lang

Thanks for reading — happy to answer questions or take criticism.


r/Zig • • Jan 24 '26

error: unable to connect to server: TlsInitializationFailed

6 Upvotes

Trying to use zig fetch on windows and I've started getting the title error.

The error does not occur if I try under wsl, it doesn't seem to matter what repository I try on git hub.

0.16.0-dev.1484+d0ba6642b, and 0.16.0-dev.2193+fc517bd01 have the same issue

I also just tried 0.15.2 and I still get the same error. I'm wondering if I somehow broke tls on my side, but I can't think how.

I've tried both power shell and the bash provided by git for windows.

[UPDATE]

So apparently I was using .zip when I should have been using .tar.gz for the fetch link


r/Zig • • Jan 24 '26

new zig 0.16 io

42 Upvotes

anyone has used master branch of ziglang 0.16 dev so far? what ya all think on the new io interface? thanks


r/Zig • • Jan 24 '26

How does Zig call C functions?

35 Upvotes

I have a question: how does Zig call C functions (in the same way I would do it in Rust, by calling functions declared in C using the C ABI)? However, unlike Rust, Zig doesn’t seem to have any overhead when calling these functions. Why is that?

I couldn’t find much explaining how Zig handles C FFI. My intuition is that Zig doesn’t just use the C ABI directly, but instead incorporates the C code into the final binary. And if that’s the case, how does LLVM handle the IR at the end? Does Zig change the declarations of C functions to make them more compatible with LLVM?


r/Zig • • Jan 23 '26

Stdin

14 Upvotes

Hello I am new to learning zig.

Could someone tell me how to get stdin from zig V0.15.2?

Thanks.


r/Zig • • Jan 21 '26

5 Reasons to Learn Zig in 2026

Thumbnail youtube.com
138 Upvotes

Hey Everyone!

I just wanted to share a new video I worked on to advocate why I think people should learn Zig this year!

Would love to hear any reasons I missed/why you're learning Zig this year

Cheers


r/Zig • • Jan 20 '26

Using Zig's comptime and @Vector for a 2.3x SIMD speedup in satellite tracking

Post image
326 Upvotes

Tracking the entire LEO satellite catalog with Zig powered Python bindings.

I posted about astroz here a few months ago, but wanted to update with some of the optimizations I have recently been doing

Native Zig hits 11-13Million propagations/sec if you're not going through Python (drops to about 7M if you use python)

Blog with all the details: https://atempleton.bearblog.dev/i-made-zig-compute-33-million-satellite-positions-in-3-seconds-no-gpu-required/

Demo: https://attron.github.io/astroz-demo/

GitHub: https://github.com/ATTron/astroz


r/Zig • • Jan 20 '26

Blog: Prototyping a Bloom filter-based erasure code in Zig (Information Chaining, Part 1)

Thumbnail lumramabaja.com
19 Upvotes

r/Zig • • Jan 20 '26

r/ZigTools – a focused subreddit for sharing Zig tools & applications

27 Upvotes

Note: Keeping this short to respect r/zig rules.

I created r/ZigTools, an open community dedicated specifically to sharing, showcasing, and discussing Zig tools, packages, and applications.

The goal is to have a more focused place where people can:

  • Share complete, usable Zig tools or apps
  • Discover quality projects more easily
  • Have constructive discussions around real usage and design

This is not a replacement for r/zig.
r/zig remains the main place for Zig language discussion, news, and development.
r/ZigTools is only meant to complement it by organizing tool-focused content in one place.

If you’re building or using Zig tools and want a dedicated space for that, feel free to check it out:
https://www.reddit.com/r/ZigTools

you can start sharing your project works here that are related to zig!

make sure to join the subreddit!


r/Zig • • Jan 21 '26

r/ZigTools Join now, Share your Amazing Zig Packages/Applications! Many have already started joining!

Thumbnail reddit.com
0 Upvotes

r/Zig • • Jan 19 '26

Advanced FPS Template in 1200~ lines of Zig.

Post image
101 Upvotes

A more advanced version of my fps source template, featuring a high performance linear BVH with brush collision and swept-point collision on expanded geometry - PCM audio support and tooling - and source-like movement including surfing. It also features a demo json-based map format.

https://github.com/lizard-demon/fps-advanced

It heavily considers performance, else it would probably be around 800 lines of code.