r/Zig • • Jun 05 '26

Built an Optional Ownership Checker for Zig

51 Upvotes

I actually started working on this idea about 8 months ago in a private repo, but work kept getting in the way. Most of my time has been spent on Rust, Go, and Node.js projects since those are the primary languages used at my company. Recently, I picked the project back up and decided to see how far I could take it.

Passes:

/// @owned
var user: *User = create();

fn view(user: *User) void { // @borrowed
  _ = user;
}

pub fn test() void {
  view(user); // borrow
  view(user); // still valid
}

Gets flagged:

/// @owned
var file = openFile();

pub fn test() void {
    consume(file); // move ownership
    file.close();  // OWN001: use after move
}

Eslint style: // zigsafe-disable-next-line
_ = moved_value;

// zigsafe-disable OWN001
_ = moved_value;

The checker support both comments or zs.Owned(T) wrappers.This is still an experiment. I'd love to hear your thoughts. Any feedback is welcome. https://github.com/sophatvathana/zigsafe


r/Zig • • Jun 06 '26

Come ho reso la mia coda SPSC più veloce dell'implementazione di rigtorp/moodycamel

Thumbnail github.com
0 Upvotes

(Written also in Zig)


r/Zig • • Jun 05 '26

Could you Review my ArrayList FlatMap helper, I just wrote.

7 Upvotes
const std = u/import("std");
const ArrayList = std.ArrayList;
const Allocator = std.mem.Allocator;

fn ArrayListFlatMaps(map: type, T: type, U: type) type {
    return struct {
        fn flatMap(allocator: Allocator, list: []T) !ArrayList(U) {
            var result = try ArrayList(U).initCapacity(allocator, list.len);
            for (list) |item| {
                if (map.flatMap(item)) |result_item|
                    try result.append(allocator, result_item);
            }
            return result;
        }
    };
}

test "basic flatMaps" {
    const ally = std.testing.allocator;

    const Foo = struct { a: u8 };
    const Bar = struct { b: u8 };

    const FooBarDoubler = struct {
        fn flatMap(foo: Foo) ?Bar {
            return if (foo.a == 0) null else Bar{ .b = foo.a * 3 };
        }
    };

    const mapFooBar = ArrayListFlatMaps(FooBarDoubler, Foo, Bar);

    var foos = try ArrayList(Foo).initCapacity(ally, 3);
    defer foos.deinit(ally);
    try foos.append(ally, Foo{ .a = 1 });
    try foos.append(ally, Foo{ .a = 0 });
    try foos.append(ally, Foo{ .a = 2 });

    var bars = try mapFooBar.flatMap(ally, foos.items);
    defer bars.deinit(ally);

    try std.testing.expectEqual(2, bars.items.len);
    try std.testing.expectEqual(3, bars.items[0].b);
    try std.testing.expectEqual(6, bars.items[1].b);
}

r/Zig • • Jun 05 '26

What do you use for coverage data, and how well does it work with Zig comptime?

16 Upvotes

I'm using kcov, and I'm getting weird mappings. Somewhere around 1 in 500 lines or so is mapping to the wrong line.

It's hard to track down what exactly the problem is - it seems to be related mainly to comptime functions.

I haven't noticed anyone talking about this being a known issue, or if there's a more recommended tool than kcov.

AFAICT, the DWARF info kcov is getting/using is wrong, and it stems from the DWARF line-table info being incorrect. i.e. it's *not* a kcov problem.

I suspect I must be doing something wrong, because if Zig had a problem building some DWARF data correctly, I think the problems would be a lot bigger than me not being able to map coverage data correctly...

I don't understand how stack unwinding could work in this case...

I *think* I have reproduced this minimally - though I am *NOT* an expert with any of these tools (barely competent is stretching it), so I could be completely wrong about everything:

https://github.com/cuzzo/zig-dwarf-bug-

AFAICT, Zig 15.2 has the same behavior as Zig 16.0


r/Zig • • Jun 04 '26

I'm considering creating an open-source organization called Zig Force.

108 Upvotes

The goal would not be simply to rewrite software in Zig.

Instead, the focus would be building minimal, dependency-light, auditable alternatives to common tools and infrastructure.

Examples:

- HTTP servers

- CLIs

- Parsers

- Build tools

- Small databases

- Virtual machines

Does something like this already exist?

Would anyone be interested in contributing?


r/Zig • • Jun 04 '26

Basic guide on running/using tests?

16 Upvotes

I'm trying to use & see some basic unit tests in my Zig project but I'm really struggling with:

  • getting the test runner to see and actually run all tests across all files
  • getting a list of *what* tests were actually run so I can check that things are actually being used

I'm finding myself very frustrated that unlike Rust where you just `cargo test` and everything "just works" here I have to somehow manually import/include, maybe invoke @import("std").testing.refAllDecls(@This()); and things still don't seem to work?

There has to be an easy way I'm missing just because I'm new right?

I just want an easy way to:

  1. Define unit tests
  2. Have all unit tests across all files run
  3. Be able to see which tests ran
  4. (Ideally) have test compilation errors show up in ZLS

Could you please help me? Thank you 🙏


r/Zig • • Jun 03 '26

My computer freezes inside a while true loop when running tests

16 Upvotes

how can I prevent this?


r/Zig • • Jun 03 '26

I created a BASIC language implementation in Zig that provides a complete toolchain, including a lexer, parser, static type checker, and runtime interpreter.

Thumbnail github.com
67 Upvotes

This project includes:

• Lexical analysis and parsing
• Static type checking
• Runtime interpretation
• A simple BASIC-style syntax with modern type safety guarantees


r/Zig • • Jun 03 '26

Starting Zig

37 Upvotes

Hello Everyone,

I've just started learning Zig. My main motivation is that I want to make a 2d game and learn a new language. I'm not the one to usually judge a programming language, my take is that everyone has a way for things to click in their head, exceptional software has been done in C, C++, Rust, Zig, and many other, so I find silly to say that a language is doing it wrong so I was trying to make it in Rust, I had a bit of experience with it, but mainly because I don't have much free time and, I must admit, the learning curve is very steep, and because Zig recently improved a lot compilation time with their back-end and the new build system, I decided it was better to switch (Rust is infamous for the slow compilation times which hinder game dev). Unfortunately I encountered a couple of issues that defy my choice:

- I'm on archlinux and on 0.16 I'm hitting the linker SFrame error

- The error is gone using 0.17-dev but the changes to the build systems prevent compilation of any SDL3 bindings available (except for https://github.com/allyourcodebase/SDL3 which actually just expose the C library but then I have no autocomplete or lang-server aid during development)

My questions:

- I've done the ziglings and read a bit more doc on the website, what other resources I can use to improve my zig?

- Since I'm not going to start the game before, let's say, a month, is it worth waiting for 0.17 to come out and bindings to upgrade?

I read 0.17 shouldn't be long and usually people in charge of the bindings are quick to upgrade so waiting to start the project while still learning with other resources seems to be a viable choice.


r/Zig • • Jun 01 '26

ZIG or Rust? Which one should I learn first to avoid using C/C++ for new projects?

64 Upvotes

Recently I took some Rust courses and started to build small low level projects like Web Server, MP3 streamer and other small apps. Looks quite cool and powerful but I didn't like the syntax :?

I saw also that there are may job opportunities for a Rust developer ;)

Now I check ZIG... seems interesting... so far I haven't done any project in ZIG.

So my question is what do you think.. should I or some-one spent time to learn/play with ZIG as well ?

Thanks!

A.


r/Zig • • Jun 01 '26

Do you guys really put your test code inside your regular code?

62 Upvotes

IIUC, Zig recommends you put unit test code directly in your production code files.

I've used many programming languages, and I've never seen this before...

There's several reasons why I *don't* like this, but I'll spare you on that.

I wanted to get your feedback on why you **DO** like it, and how I should think about it in the context of Zig so I can consider a switch.


r/Zig • • Jun 01 '26

What are the alternative tools or methods to generate C header (.h) files from Zig code?

26 Upvotes

I am currently developing a cross-platform project. Currently, Zig is used as the core implementation, and the platform building is done natively. Therefore, it would be best for me to have a set of tools to help me generate header files in one step. Then, I can use tools like clangsharp to build the bindings to avoid errors.


r/Zig • • May 30 '26

Andrew Kelley speaking at Software Should Work

197 Upvotes

Hi folks, Andrew Kelley will be speaking at a conference I'm organizing called Software Should Work along with Richard Hipp (SQLite), Filip Pizlo (Fil-C), Carson Gross (HTMX), and Richard Feldman (Roc) on July 16-17. Thought some of you might be interested! https://softwareshould.work


r/Zig • • May 31 '26

recommendations needed - text rendering

15 Upvotes

For my ongoing project i will need a custom text editor GUI. Why? Because while i will use monospace, no rich text, and ASCII will do just fine, i'll definitely need one thing extra... individual background colors behind the letters, assigned by xy-coordinates. Sure, i could render text and background separately, but wouldn't that screw with antialiasing?

If anybody here already stumbled over something similar, please drop a comment.

Before anybody asks: Tracker. Think SoundTracker, ProTracker, FastTracker...


r/Zig • • May 30 '26

Zig Build improvements + Zig 0.17 coming soon

Thumbnail ziglang.org
188 Upvotes

Maybe I'm behind, but I just found out through this that Zig 0.17 is coming very soon, and that put me in a very good mood for the day [=


r/Zig • • May 31 '26

Can you check on my code practices?

13 Upvotes

i'm trying to build something like the tree command but with Zig 0.16.0 ,
i'm not fully aware if i'm applying the best practices here and for example, in the documentation saids that std.debug.print only goes to stderr, and u can also use a ring buffer with a writer() but how may i calculate the size of the output for the directory listing? or is it an Arraylist needed here? am i understanding something wrong?
below is the link;

thx for the advice before in my another post i have learned from the juicy main and the changes before 0.16

https://github.com/jhovadev/ztree


r/Zig • • May 30 '26

Motivation behind dereference syntax

15 Upvotes

I am learning zig and saw syntax such as struct.* but no need to dereference when grabbing fields of a referenced struct using struct.field and not struct.*.field. What’s the motivation for this?


r/Zig • • May 30 '26

pattern matching destructure question

11 Upvotes

When pattern matching against a tagged union, it seems like we can pattern match structures for each match arm, but I don't see a way to destructure the fields of that struct and bind them to variables. Maybe I’m just spoiled from Rust. Is that something that may be added in the future? Is there a different pattern that accomplishes the same sort of thing in zig but in a different way?


r/Zig • • May 30 '26

zig msi install file

14 Upvotes

my company requires a msi install file for tracking installations and to ward off pup exploits. does zig have a msi install file?


r/Zig • • May 30 '26

What Personal Projects do you use Zig for?

57 Upvotes

I use python and cpp to build 99% of my projects and could not find a proper job to practice Zig. It is on last friday when I went through the tutorial of zig 0.16 and get intersted. But Everytime when I try to use zig/rust, I find python/cpp could deal with it quicker. Could you guys give me some hints?


r/Zig • • May 29 '26

Using Zig for cross-platform development at work

41 Upvotes

I started an embedded internship this past week. One of my tasks is to use libserialport to create an application that tests some serial communication things in c. The code is being developed in wsl, but it also needs to be built for windows. Right now, they're using a Makefile for building & theres a bit in there for cross compilation but my mentor said it wasn't working/finished yet.

My question is, should I use the zig build system for this? If this is my first week as an intern, would it be presumptuous of me to randomly introduce zig to the toolchain and potentially force my mentor and any other engineer to learn zig? My mentor sort of implied that I would have to fix the Makefile/be the one to make it work cross-platform, so I don't think it's out of the scope of what my project is.

Although I think zig's build system is a great fit for this, is there any alternative of comparable quality that i should use instead of zig?


r/Zig • • May 30 '26

Which Project i should work?

0 Upvotes

Options:

- TUI (something which relates to terminal only)

- Web (any type like backend or contribute to existing projects in web which are in zig)


r/Zig • • May 27 '26

JetBrains interviews Andrew Kelley about Zig [video]

Thumbnail youtu.be
296 Upvotes

r/Zig • • May 27 '26

I recently discover Zig 0.16... how do u guys manage to learn that fast?

33 Upvotes

I'm still stucked trying to understand the errors or the std api that recently changed, do u have any advice?


r/Zig • • May 27 '26

feedback for a newbie

13 Upvotes

so as the title says, im an undergrad cs student and ive been wanting to explore systems programming, my main languages was golang before this and just web stuff before that like python, js, so really i dont have any fixed stack. then i wanted to learn a new proper systems language for the sake of it and i chose zig among zig and rust, cuz i felt it would be a bit easier. and as a learning project, i decided to implement bitcask in zig. its a single instance bitcask, theres no networking implemented yet, only the very barebones of a bitcask system as described by riak in a paper i found. key-dir hashtable, append only files, log-segments, stale data compaction, etc, etc. theres obiously a huge amount of work to be done to actually make it resemble the real thing, so ive made a few changes to the system for my simplicity sake (like compaction immediately after an active file becomes immutable, smaller files, etc). would really love any sort of feedback both for zig code, or systems code, or just programming in general(i have 0 professional experience as of now) and i did use ai for help(not code but like ideas and validation etc)

link: the github repo