Skip to main content

1  Introduction

The examples of this guide come from machine learning with rtemis. Most build on one case: a resampler splits the cases of a dataset into training and test sets, by k-fold cross-validation, stratified subsampling, or the bootstrap, each with its own settings. A few types and one function run through the chapters:

Type or function Kind Holds or does Introduced in
GBM struct Settings of a gradient boosting machine Structs
Resampler enum One variant per resampling method, each with its own fields Enums
Interval struct Two ends, with a rule between them Rules
Resamples struct The training cases that a resampler drew, with a rule Rules
Window struct An Interval with a label Extending a struct
resample() generic, or trait method Draws the resamples of a Resampler Generics and methods, Traits and impls

1.1 An R8 struct

A struct is declared in R, and its declaration, its checks, and its dispatch are in Rust (Figure 1.1). A value is an R list to R code, whose fields $ reads and whose writes give new values. Its type and the hash of its declaration are in the object where R code does not reach.

R declares and uses Rust checks and runs Declare new_struct("GBM", fields = list( n_trees = type_integer(min = 1), loss = type_character( values = c("huber", ...))), rules = rule(...)) Registry one declaration, in every language fields types bounds defaults units rules version extends hash declared once, in R or with #[derive(R8)] Use x <- GBM(loss = "huber") construct x$loss read x$n_trees <- 200 write fit(x, data) call print(x) show saveRDS(x, "gbm.rds") save mirai(fit(x), x = x) send Runtime Check types, bounds, and rules Convert 200 to 200L Dispatch methods on the type Compare equality, SHA-256 hash Exchange JSON, JSON Schema Explain every problem, at its path Store a Rust struct, on request x a value of GBM n_trees 500 learning_rate 0.1 loss "huber" seed NULL Identity type declaration ml::GBM 4f2a9c... out of reach of R code Always valid Each value passes its types and rules before it exists. Immutable A write gives a new, checked value; the fields it keeps are shared. Fast Checks run in Rust and read R vectors where they are. Safe across versions A value knows its declaration; a changed unit is an error. Reproducible One hash and one JSON document per value, on every platform. One source Constructor, checks, help pages, and schema from one declaration.
Figure 1.1: An R8 struct. Teal marks R; orange marks Rust.

1.2 How R8 works

R8 is an R package whose work runs in Rust (Figure 1.2). Its R functions declare types, traits, and impls, construct values, and read, write, and print them; each passes to the binding crate r8-r in one .Call. The binding keeps the registry, which holds the intermediate representation (IR) of every declared type, trait, and impl, and reads a value’s R objects in place through RView. The IR is R8’s versioned description of a declaration, the same whether the type is declared in R or in Rust: a struct’s fields with their types and constraints, an enum’s variants, the rules, a trait’s methods, and the type’s version. R8 checks declarations, validates values, and writes JSON documents and schemas from it; r8declaration(T) returns the IR of T as a JSON document. The core crate rtemis-r8 checks declarations, validates values against their types and rules, and writes canonical forms, hashes, JSON documents, and text. A package with compiled code adds its types and methods through an exports table, which R8 registers when the package loads.

R package R8 R functions new_struct(), new_enum() generic(), new_trait() implement(), impl() GBM(...), x$name <- v r8to_json(), r8hash() format(), print() Binding crate r8-r Rust, through extendr One .Call per operation RView reads R objects in place, without copies Registry the IR of every declared type, trait, and impl Core crate rtemis-r8 Rust IR documents Declaration checks Validation and rules Canonical form, hashes JSON documents, Schema Text layout values types access A package's Rust code: rtemis-r8-api an exports table: the IR, a codec per Rust-stored type, a function per method registered at load
Figure 1.2: The parts of R8. Teal marks R; orange marks Rust.

Types between R and Rust follows one declaration and one construction through these parts.

1.3 Two styles

R8 declares behavior in two styles, which lower to the same kind of declaration and can be mixed in one package. Each style has its own part of this guide, and both parts give Resampler the same resample():

Reader Part Declares
Writes R Methods, R style A generic with generic(), and its method for a type with implement(generic, Type) <- function(...)
Writes Rust, or groups several methods under one name Traits, Rust style A trait with new_trait(), and its methods for a type with impl()

The parts Types and Packages apply to both styles.

R style resample <- generic("resample", ...) implement(resample, Resampler) <- f Rust style Resample <- new_trait("Resample", ...) impl(Resample, Resampler, ...) A trait and its impl for Resampler, in the registry resample(cv, ...) runs the impl for the type of cv
Figure 1.3: The two styles declare the same trait and impl, and a call dispatches the same way.

1.4 Values in R or in Rust

A type keeps its values in R or in Rust. The choice is separate from the style of its methods: a type whose methods are declared in the Rust style can keep its values in R, and the types of this guide do.

An R-stored type is declared in R, with new_struct() or new_enum(). Each value is an R list of its fields, whose attributes are names and class; its type and its declaration are in the object itself. x$name returns the field’s R object. x$name <- v checks the new field and the rules that read it, and returns a new list that shares the unchanged fields with the old value. The type’s methods are R functions.

A Rust-stored type is declared in Rust, with #[derive(R8)] and #[r8(storage = "rust")], in a package that compiles Rust; R8 writes the type’s R declaration from the Rust one. Each value is a Rust struct, which R holds through a handle. The first x$name asks the struct for the field and keeps the R object it returns: a vector field is the R vector the value was built from, or a view of a vector in Rust’s memory. x$name <- v checks the new field and the rules that read it, as for an R-stored value, and builds a new struct. Methods written in Rust receive the struct itself as &self; methods written in R work as for an R-stored type. R8 counts the memory the structs hold and runs R’s garbage collector as it grows.

R-stored value R memory R list names, class, and the identity name "a" values R vector 3 4 x$values is the vector passed to the constructor; x$name <- v returns a new list that shares the other fields. Rust-stored value R memory Rust memory R object, r8_rust fields read: values pointer to the struct R vector 3 4 struct Signal { name: String, values: Buffer<f64>, } borrows
Figure 1.4: A value of a struct with the fields name and values, as an R-stored and as a Rust-stored value.
R-stored Rust-stored
Declared in R: new_struct(), new_enum() in Rust: #[derive(R8)] with #[r8(storage = "rust")]
A value is an R list of the fields a handle to a Rust struct
Field types every R8 field type doubles, integers, logicals, strings, options of them, and vectors of doubles, integers, logicals, or raw bytes
x$name the field’s R object read from the struct once, then kept
Methods in R in R or in Rust
Package any R package a package that compiles Rust
saveRDS() writes the list the struct’s bytes; a value read back needs the type’s package

Validation, conversion, equality, hashing, printing, JSON documents, and mirai and future workers behave the same for both.

R8’s own operations run in compiled Rust for both kinds of value. Construction, validation, conversion, equality, hashing, and JSON are Rust code, which reads an R-stored value’s fields in place, without copying them. Keeping the values in Rust therefore does not shorten these operations, except a JSON read. It adds steps where values pass between R and Rust, each a fixed cost that does not grow with the size of the fields (Figure 1.5):

  • A new value, from construction or a field write, is a new Rust struct and an R object that holds it: R8 allocates the struct, the holding object, and a list for the fields R reads, and registers a finalizer that frees the struct when R collects the value. A new R-stored value is one R list.
  • A JSON read checks and normalizes the document, as for an R-stored value. The type’s Rust code then reads the parsed document into the struct through R8’s reader, and no R list is built, so a JSON read of a Rust-stored value takes less time than of an R-stored one.
both R-stored Rust-stored benchmark median Field write, x$name <- v Check the field and the rules that read it New R list; the other fields shared 2.2 µs New Rust struct with the new field Object to hold it, field list, finalizer 2.7 µs JSON read, r8from_json() Parse, check, and normalize the document Decode the fields into one R list 11 µs Read the parsed value into a Rust struct Object to hold it, field list, finalizer 9.0 µs
Figure 1.5: The steps of a field write and a JSON read. Both kinds of value run the shared steps in Rust; the orange steps are the fixed costs of keeping the values in Rust.

In the benchmarks, a field write takes 2.2 µs on an R-stored value and 2.7 µs on a Rust-stored one, with fields of 10 elements and of 10^6 elements alike; construction takes 4.1 µs and 5.0 µs, and a JSON read 11 µs and 9.0 µs, since a Rust-stored type’s codec reads the parsed document in place and builds no R list. A Rust-stored type gains in the work of the type’s own methods: a method written in Rust runs as compiled code over the struct’s fields, and pays the fixed costs once per call.

An R-stored type fits a type whose work happens in R, and is the default:

  • The package has no compiled code.
  • A field holds a date, a date-time, a factor, a matrix, an array, a sparse matrix, a table, a map, a JSON value, an R object, or a value of another named type.
  • Values are written field by field in large numbers, each write paying the fixed cost above, about 0.5 µs in the benchmarks.
  • A large field arrives as a compact sequence, such as 1:n: an R-stored value keeps the sequence and reads it region by region, and a Rust-stored value copies it once into Rust’s memory.

A Rust-stored type fits a type whose work happens in Rust:

  • The type’s methods are written in Rust. A Rust method reads the struct’s fields in place and converts nothing from R on each call; a method written in Rust requires a Rust-stored type.
  • Rust computes the fields. A vector that Rust allocates stays in Rust’s memory, and R reads it as a view, so a large result reaches R without a copy.
  • The type is defined in a Rust crate. The Rust struct is the one definition, and R8 writes the R declaration from it.
  • A program holds many small values. In the benchmarks, a value of 12 fields takes 440 B in R and 146 B in Rust as a Rust-stored value, and 680 B as an R-stored one.
  • Values are read from JSON documents in large numbers. The codec reads the parsed document into the Rust struct, which takes 9.0 µs for the benchmarks’ value of 12 fields, against 11 µs for an R-stored value.

Impls and types in Rust builds a package with a Rust-stored type.

1.5 Namespaces

Every R8 type belongs to a namespace, written before its name: demo::Resampler. A package’s types belong to the namespace in its DESCRIPTION field Config/R8/namespace, which defaults to the package name. Code outside a package declares into the namespace of the option r8.namespace; each chapter starts with:

library(R8)
options(r8.namespace = "demo")

A namespace has one owner, the first package to declare into it; a declaration by another package into the same namespace is an error. A package’s namespace is its name by default, so two packages that declare a type of the same name do not collide.

© 2026 E.D. Gennatas