When Zig is safer and faster than Rust: I recently wanted to compare Rust vs Zig, so I wrote a garbage collected bytecode interpreter in both languages It turned out that the Zig version was actually faster AND safer than the Rust version How come?
Garbage collection is a hard problem to solve in Rust, it fundamentally is something that doesn't play nicely with the borrow checker. It's possible to write one in safe Rust, typically using refcounting or arenas+handles, but those seem to be slower than a typical mark/sweep gc
So to compete with Zig (and also for for fun), I decided to practice the dark arts and use unsafe Rust. It turns out that unsafe Rust is hard. There are a lot of complicated rules about undefined behaviour. It's super easy to violate them and cause subtle bugs
In the end, the Zig version was not only safer and faster than the Rust version, but also much easier to write. I talk more in depth on why in my new blog post: zackoverflow.dev/writing/unsafe…
Fundamentally, Zig is a language that understands you are going to do memory-unsafe things and is designed around making that experience better and less error-prone.
Rust's language design is optimized around making it so you never need to do memory-unsafe things. I had a difficult time because almost everything I was doing was breaking the borrow checker's rules and treading in territory Rust wasn't designed for
Here are some benchmarks. Zig was 1.56-1.76x faster than Rust. The important takeaway is not that Zig was faster, but it was far easier to write.
Shout out to @munificentbob's Crafting Interpreters book (the Rust and Zig interpreters are based on the second one from his book)
@zack_overflow @munificentbob curious, do you read printed version or e-book?
@zack_overflow @munificentbob Following the same book it's amazing
@zack_overflow @munificentbob Ah, I was confused at your conclusion about Rust until I saw it was about an interpreter. For sure, to make stupid fast interpreter you NEED unsafe, and yeah it's less nice to write in Rust.