Stream: beginners

Topic: why parser/compiler implemented in Rust and stdlib in Zig?


view this post on Zulip Loric Brevet (Mar 28 2022 at 13:35):

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

view this post on Zulip Folkert de Vries (Mar 28 2022 at 13:41):

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

view this post on Zulip Folkert de Vries (Mar 28 2022 at 13:41):

Rust has much more overhead in terms of code size. It's objectively not a lot, but zig is more minimal

view this post on Zulip Folkert de Vries (Mar 28 2022 at 13:42):

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

view this post on Zulip Anton (Mar 28 2022 at 13:47):

I'll add this question to the faq.

view this post on Zulip Loric Brevet (Mar 28 2022 at 13:56):

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)?

view this post on Zulip Folkert de Vries (Mar 28 2022 at 13:57):

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

view this post on Zulip Folkert de Vries (Mar 28 2022 at 13:58):

so, roc programs should be memory safe, but does the roc compiler have to be? maybe not if it allows us to be faster

view this post on Zulip Folkert de Vries (Mar 28 2022 at 13:58):

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

view this post on Zulip Folkert de Vries (Mar 28 2022 at 13:59):

btw yes rust is safer than zig

view this post on Zulip Folkert de Vries (Mar 28 2022 at 13:59):

but also zig is safer than C, in practice

view this post on Zulip Loric Brevet (Mar 28 2022 at 14:08):

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..

view this post on Zulip Folkert de Vries (Mar 28 2022 at 14:12):

absolutely, and even if perhaps we don't use zig code as much, zig really has inspired our compiler work and goals

view this post on Zulip Folkert de Vries (Mar 28 2022 at 14:13):

e.g. data-oriented design in practice, demonstrating in-place binary patching, writing your own linker

view this post on Zulip Kevin Gillette (Mar 28 2022 at 15:53):

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?

view this post on Zulip Richard Feldman (Mar 28 2022 at 15:55):

the Roc stdlib implementation is unavoidably unsafe

view this post on Zulip Richard Feldman (Mar 28 2022 at 15:55):

we originally started out writing it in Rust, and it needed unsafe all over the place

view this post on Zulip Richard Feldman (Mar 28 2022 at 15:55):

so Rust's safety guarantees don't apply to it regardless

view this post on Zulip Richard Feldman (Mar 28 2022 at 15:56):

basically because the way it needs to interface with the compiled Roc application is using raw pointers

view this post on Zulip Richard Feldman (Mar 28 2022 at 15:56):

and dereferencing arbitrary pointers is innately memory-unsafe

view this post on Zulip Richard Feldman (Mar 28 2022 at 15:57):

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?"

view this post on Zulip Kevin Gillette (Mar 28 2022 at 16:27):

Ah, gotcha. That makes a lot of sense, thanks!

view this post on Zulip Airbus5717 (Apr 08 2022 at 13:41):

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