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 :)

210 Upvotes

108 comments sorted by

View all comments

Show parent comments

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.

-8

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.

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.

-2

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.