2 ms·
In terms of safety, Zig is somewhere in the middle between C and C++ on one side, and Rust on the other side (e.g. it enforces much more correctness than C or C
by flohofwoe 3y ago
In terms of safety, Zig is somewhere in the middle between C and C++ on one side, and Rust on the other side (e.g. it enforces much more correctness than C or C++, and has runtime spatial, but not temporal memory safety (but it has a debug allocator which doesn't recycle memory (or rather: address ranges) and which should catch most temporal memory safety problems - like your example).
Zig doesn't have separate sublanguages for macros and generics, instead these are all handled with regular 'comptime' Zig (along with type reflection).
Zig has builtin syntax sugar for dealing with optionals and error unions, which makes code less noisy (although that may be subjective - and I think Rust also got some of that over time).
Zig's interaction with the C world is excellent, and to a lesser degree also to the C++ and ObjC world (the Zig compiler can compile C++ and ObjC code, but still requires a C shim to sit between Zig and C++ or ObjC code).
Zig has much less influence from high level functional languages and less "type system wankery" compared to Rust, this again is a good thing for some people, while disliked by others.
A minor detail, but which I think demonstrates the Zig philosophy: the Zig compiler executable is also the build system and package manager. In Rust this is delegated to a separate tool (cargo).
...and my personal opinion: Zig is a lot more 'elegant' than Rust which results in more joy when writing Zig code. Rust feels entirely too much like 'designed by committee'. With Zig I "grokked" the language in a weekend of tinkering, while with Rust I never really had "fun" despite several attempts of trying to like it.
TL;DR: If I had to create an absolutely waterproof and critical isolated piece of code, I would probably write that in a "simple Rust subset". For anything else I would definitely prefer Zig.
- throwawaymaths 3y agoIs memory safety the only thing holding you back? Suppose there were a way to do static borrow checking, as a 3rd party tool?
- naruhodo 3y agoThe C/C++ interop in Zig gives it a great value proposition for embedded systems programming. My experience of that culture has been that C++ is "too unpredictable", so C is the language of choice. And glob help you if you want to use dynamic memory allocation. Zig could replace C and the preprocessor and support programming at a higher level of abstraction (generics). I've tried a couple of times now to get my head around Rust with small efforts. Apparently I need big efforts and deep immersion. "A half-hour to learn Rust"[1] certainly helped with understanding the syntax around lifetimes. I keep reading that understanding the borrow-checker and lifetimes should be a snap for C/C++ programmers. Well, I've been doing C++ for over 30 years and C for longer, and Rust just strikes me as a bondage and discipline language. [1] https://fasterthanli.me/articles/a-half-hour-to-learn-rust https://fasterthanli.me/articles/a-half-hour-to-learn-rust
- lioeters 3y agoInsightful, thanks! I was wondering about the pros and cons of Zig vs Rust, and your comment shines light on various angles. It confirms how I unconsciously felt about Zig, that it's a well-designed language which succeeds in reducing the complexity of concepts to learn, and achieves a good balance of simplicity, fun, power, and cleverness.