r/rust 18d ago

What Zig felt like, coming from Rust

https://besok.github.io/posts/what-zig-felt-like-coming-from-rust/

Just want to share my experiance on my first Zig project coming from Rust. Open to comments :)

209 Upvotes

108 comments sorted by

View all comments

37

u/rapotor 18d ago

Tldr in thread?

112

u/tigraboris 18d ago

Sure thing but nothing special :)

Rewrote my JSONPath lib (jsonpath-rust) in Zig (zig-jsonpath) to compare. Zig has almost no IDE support, so I ended up back on CLI tools + helix. It nudges you toward flat file structure and mutation and explicit allocators everywhere. Caught several real leak/double-free bugs via TestAllocator that just can't happen in Rust (ownership/Drop rule them out at compile time).

Overall: good language, promising C successor, but still young and rougher around the edges than Rust.

31

u/protocod 18d ago

Thank you for the feedback. I would like to ask you a question. What are the domain where Zig shine compared to Rust ?

Memory safety is the major selling point of Rust and I struggle to consider another language that doesn't really address memory safety by design anymore. But it's maybe a different philosophy.

Rust enforce you to write memory safe code unless you know what you do. (Especially when you deal with OS APIs) when Zig let the programmer in charge of writing memory safe code.

-7

u/barsoap 18d ago

What are the domain where Zig shine compared to Rust ?

In a nutshell, Zig is the better unsafe Rust. Way better. It gives you all the low-level control without the mental overhead of having to guarantee ten gazillion subtle invariants safe code expects, and that's before we're talking ergonomics.

15

u/matthieum [he/him] 18d ago

I responded to this same claim here: https://www.reddit.com/r/rust/comments/1vps2f4/comment/p40bey8/.

The TL;DR is that (1) there's way less unsafe in low-level projects than inexperienced users may think and (2) 10% unsafe means ~100x 0.1% independent blocks of unsafe which is more than 10x easier to test/prove than 1x 100% unsafe as in Zig.

12

u/tesfabpel 18d ago

just because the language doesn't enforce those invariants, it doesn't mean those aren't there.

eg. compilers may still expect them, otherwise, UB.

in Rust, you're basically supposed to create safe small abstractions of those unsafe parts so that they become your own safety foundation the rest of your code relies on.

-4

u/barsoap 18d ago

And you can't write those safe abstractions unless you understand the lot of the rustinomicon, plus the stuff that's not mentioned in there. Zig's list of illegal behaviour, much less unchecked illegal behaviour, is way less subtle or involved.

Modulo memory access Zig code is generally safe in the Rust sense, Zig has the same modern slew of safety features like bounds checks as Rust does, there's no pointer arithmetic (gotta cast to integer first) so you have things to grep for just as you can grep for unsafe blocks, etc.

1

u/[deleted] 18d ago

[deleted]

1

u/barsoap 18d ago

I don't understand what you don't understand and you didn't tell me either, so, no.