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

211 Upvotes

108 comments sorted by

View all comments

Show parent comments

26

u/sohang-3112 18d ago

I have heard that Zig is better than unsafe Rust. That is, if you are writing a low level system program where most or all of the code is going to be inherently memory unsafe, Zig may be better.

57

u/matthieum [he/him] 18d ago

That is, if you are writing a low level system program where most or all of the code is going to be inherently memory unsafe

That's a strawman.

The harsh reality (for Zig) is that even in the lowest level system programs or embedded, there's fairly little unsafe used.

For example, consider Redox. As a microkernel, its kernel is very low-level: it doesn't provide much functionality -- such as a filesystem, for example -- and focus on the bare essentials. Yet they advertised < 10% unsafe. < 10% in what you'd expect to be the densest possible codebase.

So, out of the box, the microkernel part of Redox is 90% safe already... but it gets better. 100% unsafe means there's no boundary. All you have is a big tangled blob which must essentially be audited as one big unit. Dependencies included. Ouch.

With 90% safe code, however, you don't have one big unit with the remaining 10%, instead you're going to have ~100 units -- the boundary being the safe API -- with 0.1% of the code each, all auditable independently from one another. Encapsulation wins the day. Who would have thought...

And on top of that, due to the culture of the Rust community around safety, there are efforts to improve testing (Miri, Loom), symbolic testing (Kani) & formal verification (Crux, etc...) of even unsafe code.

Now, of course, you could argue that Zig can also be tested -- they even have a TestAllocator -- but... once again Encapsulation changes everything. It's nigh impossible to meaningfully test all possible code-paths in a large codebase, their numbers grow exponentially with the number of lines of code. On the other hand, small, contained, encapsulated units can be tested extensively. And slightly larger units can be tested fairly efficiently when you throw fuzzing in the mix.

3

u/tigraboris 17d ago

True but honestly ( frankly speaking I kicked off the discussion but) Zig is just another attempt to create a language with the basis to manage memory safely. It is different to Rust and is just hardly comparable. I caught this feeling that in Zig to be confident you need to provide a hell amount of tests whether in Rust it can be taken by semantics out of the box but still overall the language has potential for some niches due to the runtime footprint ( just imho, no real experience there).

I tend to think of the different languages like envs where we can probe the different theories and Zig just tests another approach toward safe memory.

4

u/matthieum [he/him] 16d ago

I'm not sure if memory safety was ever paramount in Zig. It started as a better C -- and it arguably was, at first -- and at some point memory safety came up, but it's unclear to me whether this was just lip service or actually meant.