17.3 Why Rust?
17.3.1 Question
Which language should I use for a new R interpreter: Rust, C++, Go, or something else?
17.3.2 Context
A new interpreter does not inherit GNU R’s technical debt. It can make different design choices, and it can put per-interpreter isolation and explicit state ownership first.
The language decides a lot of that up front. Memory management, concurrency and error handling all start as language-level choices, and the language decides which classes of bug the compiler can rule out.
17.3.3 Constraints
The interpreter has to support R’s semantics: lazy evaluation, copy-on-modify, first-class environments, and dynamic typing. It also has to be fast enough that the overhead does not make R code noticeably slower than in GNU R.
Packages need R’s C API eventually, which means a foreign function interface (FFI) between the interpreter’s language and C. Its overhead and its complexity depend on the language.
Data races and undefined behavior are unacceptable. The interpreter must not silently corrupt data or crash on valid input.
17.3.4 Options considered
C++. C++ is mature, widely used, and close to the C that GNU R is written in. It does not prevent data races, and undefined behavior is always possible. Concurrency needs manual synchronization, and the mistakes are easy to make and hard to see.
Go. Go has built-in concurrency through goroutines and channels, and its race detector finds races in tests. It does not prevent them. Go also has its own garbage collector, so an R interpreter in Go would run two collectors: one for R objects, and Go’s underneath it. And cgo restricts how long C code may hold pointers into Go memory, which an R C API built on Go objects would run into.
Zig. Zig is a newer systems language with good C interop and explicit memory layout. It catches some memory errors at runtime, in its safe build modes, but has no compile-time ownership checking, and it has not reached 1.0.
Rust. Rust rules out data races in safe code at compile time, through ownership and the borrow checker. Each value has one owner, references are borrowed, and the Send and Sync traits decide what may cross or be shared between threads. Rust has no garbage collector, unsafe code is marked and confined, and FFI with C is well developed.
17.3.5 Decision
I implement rrs in Rust, with unsafe forbidden in the interpreter crates.
When I made the decision, unsafe was forbidden in the whole workspace. The C API changed that, as recorded below.
17.3.6 Reasons
Data races are the main concern. A multi-interpreter design needs explicit coordination between interpreters, and in rrs each interpreter is a separate value owned by one thread. The type system enforces that: code in which two threads use one interpreter at once without synchronization does not compile. The check happens at compile time, before anything runs.
In C++, nothing stops a programmer from sharing an interpreter between threads and forgetting the lock, and the resulting race corrupts data without any error.
Each rrs interpreter is one value owned by one thread. The P4 concurrency demo moves an interpreter built on the main thread to a worker thread and keeps computing there, which exercises Interp: Send at runtime as well as at compile time.
Ownership also makes the architecture readable: the code shows which thread owns which data.
Rust has no garbage collector of its own, so rrs’s collector is the only one. rrs keeps R objects in an index-handle heap with a precise mark-sweep collector, and the workspace rules out Rc-based object models and garbage-collection crates, so the lifetime of an R object belongs to the R collector alone.
R’s C API needs good FFI, and Rust’s C interop can both call C and be called from C. That has held up: CRAN packages compile from source against rrs’s C API crates, and Rcpp builds against them.
Unsafe code is marked and can be confined. In rrs the interpreter crates contain none, so a memory-safety bug there would have to come from a dependency or from the C API crates.
17.3.7 Consequences
The borrow checker is unfamiliar to C programmers, and learning it takes time.
Compile times are longer than for C. A full rebuild takes noticeably longer, which slows the edit and test cycle.
Rust’s library ecosystem is smaller than C’s or C++’s. For rrs that has not mattered, because the two lowest crates need almost nothing: rrs-syntax has no dependencies and rrs-runtime uses only rustc-hash, and the lexer and parser are written by hand. The evaluator does pull in a regex engine and compression crates.
The P10 baseline measures rrs against itself, before any optimization. The later P41 to P43 notes compare rrs with GNU R 4.6.1 directly: at the end of P43 rrs was slower on scalar loops, the sieve and vector arithmetic, and faster on closure allocation, string building and environment churn.
Safe Rust still allows global state, through a static holding a Mutex or an atomic, so the language alone does not keep rrs free of it. A gate does: it greps for statics and keeps the interpreter’s allowlist empty. The C API crates need thread-local state, because R’s C API passes no interpreter argument, and each such use is listed with its reason.
The workspace lint unsafe_code = "forbid" applies to the interpreter crates. Four of the five C API crates opt out of it, and a gate rejects unsafe anywhere outside all five. Thread-local state is also allowed only in those crates, each entry with its reason in scripts/ci/global-state-allowlist.txt.
17.3.8 Evidence that would change my mind
I expected the C API layer could be built without relaxing the workspace-wide unsafe_code = "forbid". It could not: code that speaks C needs unsafe, so the C API crates have it and nothing else does. The condition I now check is narrower. If unsafe spreads past those crates into the interpreter, the safety argument becomes hollow, and I reconsider.
If compile times become unacceptable, I reconsider. Incremental compilation helps, and so far the compiler has not become the bottleneck.
If a language appears that is safer than Rust and as fast, I reconsider. None of the alternatives above is: C++ is less safe, Go adds a second collector, and Zig has no compile-time ownership checking.
Where this appears
- r-in-rust (rrs) Foundational language choice that shaped the implementation approach.