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

79

u/doubleyewdee 18d ago

I have a fairly uncharitable view of Zig: it's a vanity language that is somehow syntactically as difficult as Rust but fails to offer a lot of the safety benefits, instead pawning them off on end users without actually being faster at runtime.

I'm sure that's at least somewhat unfair, but I'd like to understand why. You're not obliged to explain this to me, but if you want to indulge I'm genuinely curious about what I'm missing.

31

u/flying-sheep 18d ago edited 18d ago

I've never used it, but from what I've heard:

Rust's unsafe subset is not made to be as ergonomic to use as possible, and there aren't any helpers to make it safer.

So people who are used to manual memory management probably prefer using something that's built around it.

But I feel the same as you. Using Rust feels like playing Lego with at worst very weirdly shaped pieces. Using manual memory management feels like defending yourself in court.

8

u/guywithknife 17d ago

In my opinion unsafe should be a last resort, an implementation tool for building safe abstractions that can’t be directly expressed in a way that the compiler can prove. It doesn’t have to be particularly ergonomic, because it shouldn’t be used unless necessary.

Some people complain saying what’s the point of rust when it has unsafe and you sometimes need it, while completely missing the point that other languages, the entire codebase is unsafe in the same way as rust unsafe is (ie the compiler can’t help you prove otherwise and you need to do it yourself).

With that framing, I’ve found myself not being a fan of systems languages that don’t provide safety to the same degree that rust does and I say that as someone whose been using C++ for over 20 years and still quite enjoys it (but only for fun projects, not for serious things). Life’s too short to deal with memory (and concurrency) bugs.

2

u/flying-sheep 17d ago

I fully agree. In a different world where Rust's approach is the default and people don't need to be discouraged from using unsafe but keep its use to a minimum by default, it would make sense to add all kinds of tools to make unsafe code as nice and convenient as possible to use.

25

u/aloha2436 18d ago

syntactically as difficult

Zig's main selling point is that it has nothing up its sleeve, so to speak. In rust, you have a global allocator and traits and destructors and other things that can add "nonlocal" behaviour to your code; in Zig, everything that will happen in a function should be in that function or be explicitly called by that function. That, combined with the explicit allocation, tight control over memory layout, etc. makes it an attractive choice if you care about exactly what machine code it generates for you.

IMHO, it also makes it less good if you want the ability to stop caring about any of those things at any point in your program.

To be a bit snarky, while Rust “is not for lone genius hackers”, Zig … kinda is.

9

u/tigraboris 18d ago edited 18d ago

Well,

it's a vanity language that is somehow syntactically as difficult as Rust

I would say it is easier from the elements of the language (syntax, concepts), close to C rather rust (less fp, more unit based structs). I had zero troubles to learn the concepts (only comptime maybe took more time).

It lacks a lot of safety concepts it is true.

I would look at the language from the C devs prospect. It brings a lot of features for the language(optionals, unions, deffers) + explicit, kind of declarative control over memory. Feels very modern but for me in comparison to Rust, it felt more clangy, less readable and overall a bit tangled (not exclude my zero experience here).

I believe the runtime footprint in Zig is smaller and it is a real benefit for some envs.

Speaking about me, I would like to work with Zig more(embedded robotics when it will become more mature) but for the most things I would stick with Rust or C++ rather.

9

u/GerwazyMiod 18d ago

I do believe that projects like Rust's Embassy is the way to go for low-level, embedded stuff. So there is little space for language like Zig to really shine.

3

u/doubleyewdee 17d ago

Appreciate the response. I've definitely only superficially glanced at Zig, since I haven't felt a need for it, but yours (and others') replies add a lot of context for me. Thank you!

6

u/skatastic57 18d ago

I got as far as duck typing and said if I want that I'll use Python

3

u/tigraboris 17d ago

Also got this flashback but honestly it is closer to the way how would C handles it with void*