18.2 r-in-rust (rrs)

PROJECTSSTATUS: DEVELOPINGLEVEL: INTROREVIEWED: 2026-11-18

r-in-rust (rrs) is an interpreter for R written in Rust. The lexer, parser, evaluator, heap and collector are its own. Where matching GNU R byte for byte is easiest with GNU R’s own code, rrs vendors that code unchanged: R’s numerical library (nmath, in C), all of grid’s R source, and the R source of 671 more functions from GNU R’s packages, which it loads on first use. Each interpreter is one value owned by one thread, and it carries its own heap, garbage collector, symbol table, string cache and random number stream. The interpreter crates hold no global mutable state: a gate check greps for statics and keeps their allowlist empty.

The largest result is that R’s C API works on rrs. Packages compile against C headers that present R’s function-accessor flavor, so SEXP stays fully opaque and a package that goes through the accessors finds everything it needs. A package that includes R’s non-public headers finds nothing, because there is no struct layout to reach into. CRAN mime compiles from its own source tarball, installs, loads through library(), and its routines answer. Rcpp builds against the headers and loads. In the census of the thirty most downloaded CRAN packages with compiled code that docs/ledger.md records for 2026-08-18, twenty-eight install and all twenty-eight load. The two that failed are StanHeaders, whose failure the ledger suspects is a file-system flake on the Windows drive under WSL, and rstan, which needs it.

The interpreter crates are written in safe Rust and forbid unsafe at the lint level. The five C API crates exist to speak C and are the only ones allowed the keyword, and the only ones allowed thread-local state, because R’s C API passes no interpreter argument. A gate names them and rejects unsafe anywhere else, and every allowed use of state carries its reason in scripts/ci/global-state-allowlist.txt.

The scope goes well past the early phases: vectors, matrices and n-dimensional arrays, data frames, factors, dates, complex numbers, closures, promises, conditions via tryCatch, S3, and S4 with setClass, new, slots, generics and single dispatch, the exact Mersenne-Twister RNG and distributions, substitute, match.call, quote, eval, and a precedence-aware deparser, lm() and glm(), an SVG graphics device on which ggplot2 draws scatter plots, bar charts and faceted plots, mclapply over worker interpreters with no shared heap, and namespaces with R’s real three-environment structure, DESCRIPTION and NAMESPACE parsing, dependency resolution, install from source, RDS and lazy-load. Differential tests compare output byte for byte with a pinned GNU R 4.6.1, svn r90187. The P4 concurrency demo, eight interpreters on eight threads, ran under ThreadSanitizer with zero race reports, and so did mclapply.

18.2.1 Current state

As of 2026-10-17 (docs/README.md), rrs has 2,780 of the 2,951 functions that GNU R’s shipped namespaces export, 94.2 percent, counted by name in docs/coverage.md. Behavior is checked separately, by 1,743 corpus cases diffed against the pinned GNU R 4.6.1. The C API serves compiled CRAN packages through opaque headers: mime compiles from source, installs and loads, Rcpp builds and loads, and twenty-eight of the thirty most downloaded compiled packages installed and loaded in the 2026-08-18 census. Among the 171 names absent on that date were eigen() and svd() (docs/absent.csv); the P78 LAPACK work added both and reached main on 2026-11-15.

Matrices are atomic or recursive vectors with a dim attribute, and data frames are lists with class data.frame, both as in GNU R. rrs copies R’s object model exactly, because its tests compare its output with a pinned GNU R byte for byte.

18.2.2 Lessons

The C API result bears on my other projects. A runtime with no SEXPREC at all can serve compiled packages, as long as they go through R’s accessor functions. Because rrs compiles packages from source against its own headers, it needs source compatibility with R’s C API and no binary compatibility with GNU R. The concurrency result is less surprising: with nothing shared between interpreters, ThreadSanitizer had nothing to report.

18.2.3 Repository

The r-in-rust repository is not public yet.

Further reading

Where to go next

Back to top