Hi, it is been a while I haven’t connected to the Zulip, but I come back with a question that popped.
What were the reasons that led to choosing Rust to develop the compiler, and Zig to implement stdlib?
Why not choose the same language for both? At the first glance, it would make sense because the entry for new Roc contributors would be cheaper.
And same but different question (as I am currently getting more and more interested in Zig): could Zig have been chosen for the development of the compiler? And if not, what is lacking in Zig for that purpose?
Trying to understand the context and the history here :-)
Thanks a lot
at the time we 1) did not really know zig well and 2) it wasn't ready. Even now, it's not actually meant to be used in production. The reason zig entered the project because it has many different backends (wasm, various assembly formats, llvm IR) and can create code with minimal dependencies
Rust has much more overhead in terms of code size. It's objectively not a lot, but zig is more minimal
but, we think rust is a nicer language to work in for a project of this size. It has a type system that we're more familiar with, it has a package ecosystem, excellent tooling
I'll add this question to the faq.
Thanks @Folkert de Vries for the quick answer and yes @Anton that’s a great idea.
When comparing Zig and Rust, one aspect that often comes up is that Rust has safety as a first concern and this makes Rust "safer" than Zig. Do you agree with that statement?
And if so, was this also considered in the choice (as I guess safety matters quite a lot for a programming language such as Roc)?
it matters for the end product, but less for a compiler. Compilers should be fast and give good error messages. If they leak some memory, that's not the end of the world really
so, roc programs should be memory safe, but does the roc compiler have to be? maybe not if it allows us to be faster
the next version of zig will be self-hosted, showing that you can build a compiler in zig, and a very fast one at that
btw yes rust is safer than zig
but also zig is safer than C, in practice
it matters for the end product, but less for a compiler.
This makes sense effectively.
Really looking forward to the self-hosted version of Zig. This seems to be a huge milestone for the project. It will be interesting to see the speed difference of the compiler after the rewrite. And having finished that will release some workforce to stabilize/improve stdlib, and maybe add some more runtime safety checks to the allocators before v1.0.0 (to try to catch up a bit with Rust in that regard)!
The future is promising, and this will profit downstream projects such as Roc..
absolutely, and even if perhaps we don't use zig code as much, zig really has inspired our compiler work and goals
e.g. data-oriented design in practice, demonstrating in-place binary patching, writing your own linker
It still seems like there's still some open questions, at least that I have:
If runtime safety is more important than the safety of the compiler code, why then use Zig for the parts that will directly end up being used by Roc at runtime, while using Rust for the parts that won't end up being used by Roc at runtime (why not the reverse)?
If Zig will be getting safer, does that mean we will potentially explore using Zig in the compiler?
Are the benefits of using both, at the expense of more build requirements, and as @Loric Brevet mentioned, more languages for contributors to learn, worth keeping two languages involved in the core of the implementation?
the Roc stdlib implementation is unavoidably unsafe
we originally started out writing it in Rust, and it needed unsafe
all over the place
so Rust's safety guarantees don't apply to it regardless
basically because the way it needs to interface with the compiled Roc application is using raw pointers
and dereferencing arbitrary pointers is innately memory-unsafe
so when we were having a bad experience using Rust for the stdlib, the thinking was "since we're not getting any benefit out of Rust for this, maybe we should write it in C?" and then "at that point, why settle for C when we could use Zig?"
Ah, gotcha. That makes a lot of sense, thanks!
Zig is actually a super nice language. I'd say its best language for the platform
Last updated: Jul 05 2025 at 12:14 UTC