There are endless debates online about Rust vs. Zig, this post explores a side of the argument I don’t think is mentioned enough.

Intro / TLDR

I was intrigued to learn that the Roc language rewrote their standard library from Rust to Zig. What made Zig the better option?

They wrote that they were using a lot of unsafe Rust and it was getting in their way. They also mentioned that Zig had “more tools for working in a memory-unsafe environment, such as reporting memory leaks in tests”, making the overall process much better.

So is Zig a better alternative to writing unsafe Rust?

I wanted to test this myself and see how hard unsafe Rust would be by building a project that required a substantial amount of unsafe code.

Then I would re-write the project in Zig to see if would be easier/better.

After I finished both versions, I found that the Zig implementation was safer, faster, and easier to write. I’ll share a bit about building both and what I learned.

  • nous
    link
    fedilink
    English
    arrow-up
    8
    arrow-down
    1
    ·
    8 days ago

    That’s true for your application maybe, but they go on to say how one should consider whether or not their problem is going to fit well within the rules of the rust borrow checker and that needs to be talked about more (vs just assuming Rust is the safest option).

    Yeah, that is why I was calling out their title as click bait. Saying Rust vs Zig will mean most people think of safe rust vs zig. But the article is about unsafe rust vs zig which is a completely different story IMO. If you need lots of unsafe zig might be better - but the title does not say that. Hence IMO it is clickbaity.

    Rewrites aren’t often done outside of hobbyish projects because they’re very expensive, stop new feature development, and you really can end up with something that’s worse than what you started with (this is especially true if you’ve switched languages or frameworks).

    Ok I will give you that for big projects. But this is on the other side of that. If you write two things in quick succession you will more likely still have the problems encountered in mind and be better able to navigate them the second time than if you are part of a large team that has had a lot of turn over since the project was first written. And you might make different mistakes - but the second round of mistakes is normally less impactful then the first set.

    They do explain with citations why it makes more sense (i.e. you end up with something more performance) to write their VM outside of the restrictions of the borrow checker.

    Oh yeah, of course this case I can see all that being true. But fundamentally they chose this problem because of that given they said:

    I wanted to test this myself and see how hard unsafe Rust would be by building a project that required a substantial amount of unsafe code.

    Sounds like they want to compare unsafe rust with zig. They started with that idea which from my experience is not typical of most applications so their findings are not either.

    I think the claim is a bit of a stretch off the cuff.

    Yeah that one might be.

    Though unsafe rust overall is not a large amount of what rust code is, the author does seem to be picking the topic based on the hardest parts of rust and their title talks about all of rust which IMO is an unfair comparison. The story of zig being better than unsafe rust is interesting but only part of the whole zig vs rust debate.