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@programming.dev
    link
    fedilink
    English
    arrow-up
    24
    arrow-down
    1
    ·
    edit-2
    11 days ago

    Title is clickbait. They only talk about unsafe rust, which I can see zip being safer/easier than unsafe rust. But 99.9% of code I write is safe rust - which most people just call rust. Even the author calls out the vast difference to writing safe vs unsafe rust:

    Overall, the experience was not great, especially compared to the joy of writing regular safe Rust.


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

    Of course it will be. The second time you write any project it will be easier and faster as you learn a lot form the first time you write something. If zig is always the rewrite it will come off better. Almost all rewrites are better or faster, even if you are moving to a slower language - the language makes a difference to performance and ease of writing. But far more does how you write things and the data structures/algorithms you use.

    Overall they seem to want to write as much unsafe as they can and are writing rust like it is C. This is not a good idea and why zig will be better suited. But you can write a VM without large amounts of unsafe if you want to and it can be performant. Unsafe can be used in small parts where performance matters and cannot be done without it (though this is not that common I find).

    • BehindTheBarrier@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      11 days ago

      I don’t think the title is that clickbaity, since it’s pointing out that there are places where Rust falls short. As with all tools, you should know when to not use them. If you know you will end up with a lot of unsafe code, then Rust may not be a good choice.

      The TLDR doesn’t say it but the article specifically points out the problem is when trying to use mark-sweep garbage collection in the VM, and talks about the problems that arise when you are forced to use C-like writing to combat the Borrow Checker. Apart from the rewrite in Zig being able to iterate on experiences from the Rust version, I think the points made out were perfectly good arguments about why writing unsafe Rust is far from a good experience. I see that experience reflected in the choice done for the Roc standard library too.

      • nous@programming.dev
        link
        fedilink
        English
        arrow-up
        6
        arrow-down
        1
        ·
        11 days ago

        IMO it is clickbaity because it promises to compare rust and zig, but in reality it is just comparing unsafe rust and zig. IMO all it really needed to not be is the word unsafe in the title like they use everywhere else in the article. That is fundamentally my only problem with it. I do agree with your other points on knowing when a tool is good or not to use but I would have been much less likely to click on it if it mentioned unsafe in the title - I already know rusts unsafe is not the best and was expecting some arguments around the advantages of zig over safe rust -ie what most people write, not a small subset of the language.

        • BatmanAoD@programming.dev
          link
          fedilink
          arrow-up
          2
          arrow-down
          2
          ·
          11 days ago

          I don’t think it’s fair to consider unsafe Rust such a small subset of the language that it requires calling it out as a separate thing from “Rust” in the title of an article. Unsafe constructs are necessary in the standard library and many crates, whether or not you use it in the code you actually write.

  • sudo@programming.dev
    link
    fedilink
    arrow-up
    3
    ·
    10 days ago

    The article title is poorly written but the conclusion is pretty sound: If you’re planning on writing unsafe code, use a language meant for unsafe code. Zig is meant for unsafe code, Rust isn’t.