Rust code in CRAN packages?
The purpose of this vignette is to compare and contrast usage of C++ and Rust for efficient numeric algorithms, in an R package.
Example: isotonic regression
In summer 2026, I taught ROP631, nonlinear programming. One example application that I discussed was optimal label positioning. In a figure with several curves, we want to draw a text label at the end of each curve, subject to constraints that ensure readability:
- each label does not overlap neighboring labels, and
- first and last labels do not go outside of plotting region.
These rules are implemented in R packages directlabels and animint2.
Until earlier this year, both R packages used quadratic programming to solve the optimal label positioning problem.
During 2011–2026, directlabels called quadprog::solve.QP() to compute optimal label positions.
This code worked fine, but it is a generic QP solver (cubic memory and quadratic time), and I was always wondering if we could use a more efficient algorithm.
On 12 Aug 2026, I typed up a change of variables that could be used to frame the label position problem as an instance of isotonic regression. There are lots of different solvers available for isotonic regression. Most are instances of the Pool-Adjacent-Violaters (PAVA) algorithm. I am advising Justin Quiron this semester on an investigation of which is most efficient. His preliminary results are in the pool-party repo.
On 13 Aug 2026, I typed up a dynamic programming algorithm for solving isotonic regression in linear time. On 27 Aug 2026, I merged C++ code for this algorithm into directlabels, to be used instead of quadprog. This resulted in asymptotic speedups. I submitted it to CRAN as directlabels version 2026.8.27. The goal of this blog is to investigate if this simple algorithm can be coded in Rust and deployed to CRAN just as easily.
Rust and C++ in R
C and C++ are the best-supported compiled extension languages for R, because R itself is coded in C.
For another project about a more efficient implementation of local linear smoothing, the base R stats::loess() calls Fortran code.
Don Yin suggested coding a new version using Rust, and I replied that C++ has several advantages:
- C/C++ is definitely more widely used than Fortran/Rust, so C/C++ should be preferred if the goal is to maximize the number of people who could read / understand / maintain the code.
- Also C/C++ is definitely more portable (easier to get accepted on CRAN, easier for others to install) than Rust. For example, the Rust in R guide to cran-publishing says you need to vendor Rust crates, meaning that you need to include a copy of the Rust code of all Rust dependencies that you use. This can increase the size of your R source package, triggering a
R CMD checkwarning about package size that prevents automatic acceptance of new CRAN submissions of your package. - If you write C++ code the “right” way (https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) then there are no memory issues. In practice this just means avoiding new/delete, which do allocations outside of the R memory. (We should use R to do memory allocations also because we want to benchmark the memory usage with atime)
Re-coding dynamic programming for isotonic regression in Rust
TODO
misc
The CRAN’s Rust versions page has a list of versions of rustc and cargo on each of the CRAN check machines.
cpu and version info
sessionInfo()
## R version 4.6.2 (2026-10-29)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.5 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=fr_FR.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=fr_FR.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=fr_FR.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/Toronto
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] compiler_4.6.2 cli_3.6.6 tools_4.6.2 otel_0.2.0 knitr_1.52
## [6] xfun_0.62 rlang_1.3.0 evaluate_1.0.5