19 Rust for Runtime Engineering
This section is not a Rust tutorial. Each page teaches Rust through runtime problems: who owns a value, who can change it, and what the compiler can prove about thread safety. What the compiler can prove is also why r-in-rust forbids unsafe code everywhere except the crates that implement R’s C API.
In C, a programmer gets memory safety and thread safety through discipline, and the language offers no help; entire books teach how to avoid use-after-free and data races. Rust moves this burden to the compiler. A static analysis proves that a value has one owner and that all borrows obey a single rule: sharing and mutation cannot coexist.
For a runtime engineer, that shift changes the job. An interpreter holds mutable state: a heap, a symbol table, a call stack. Threads share these resources or access them sequentially. Rust forces a design choice early: is this interpreter per-thread or shared? If per-thread, ownership is simple. If shared, the cost of synchronization is explicit in the types.
Ownership is the foundation. All other concepts build on it. Borrowing and lifetimes extend ownership to temporary access. Send and Sync make thread safety a type property. Interior mutability and shared ownership solve the cases where single ownership is not enough. Enums and pattern matching represent dynamic values safely. Unsafe Rust allows crossing to C when necessary, but marks the boundary clearly.
Reading this section requires no prior Rust knowledge. But you need to understand what memory safety means and why thread safety matters. Start with Orientation if those concepts are unfamiliar.
After this section
After this section, you can explain what ownership means. You can explain why it matters for parallelism.
You can describe borrowing and lifetimes. You can use them to prevent dangling references at compile time.
You can distinguish between Send and Sync. You can explain what they guarantee.
You can use Rust types that express concurrency safety in the type system. You can apply Rust idioms to runtime engineering problems.
You can understand when and why unsafe code is necessary at language boundaries.
The pages
Ownership. Who owns a value, what they can do with it, and why Rust enforces this in the type system. This is the foundation.
Borrowing and Lifetimes. Temporary access to a value without ownership, and how the compiler proves a reference stays valid.
Arc, Mutex, and Interior Mutability. Explicit ownership choices for shared data and the cost of each choice.
Send and Sync. Marker traits that make thread safety a type property and allow the compiler to reject programs with data races.
Enums and Pattern Matching for Runtime Values. Tagged unions as a safe way to represent dynamic values and exhaustive checking that all cases are handled.
Unsafe Rust at the R Boundary. What unsafe permits, why it is necessary to cross to C, and how r-in-rust confines it to the crates that implement R’s C API.
Before this section
The Orientation page linked above and Race Conditions describe the problems Rust solves. Read them first if those problems are new to you.
Where this leads
Every page in this section applies to the r-in-rust project. The section on Memory provides foundation for Rust’s memory model. Concurrency and Parallelism expands on thread safety beyond what Rust alone can offer.
Reading order
Start with Ownership; every other page rests on it. Next, read Borrowing and Lifetimes, which extend ownership to temporary access without moving the value.
Arc, Mutex, and Interior Mutability covers the tools for when single ownership is not enough, and what each one costs. Send and Sync comes after it, because it explains which of those tools may cross a thread boundary and how the compiler rejects the ones that may not.
Enums and Pattern Matching shows how to represent dynamic values with their types checked, which is how an interpreter stores R values. Finally, Unsafe Rust at the R Boundary covers what happens when safe Rust has to call C, and where the compiler’s guarantees stop.
Motivation
Rust is not a simpler language than C. It has more concepts, more syntax, more rules. But those rules buy something valuable: the compiler proves properties that C programmers must verify by hand.
For an interpreter, that proof covers a lot. Safe Rust code cannot cause a use-after-free or a data race, and the compiler rejects programs that try. It does not rule out deadlocks, logic errors, or bugs in unsafe code.
This costs effort at design time. Ownership must be decided early. Lifetimes must be written out when the compiler cannot infer them across function boundaries. Sharing data across threads requires explicit type choices.
Key differences from C
In C, the programmer has to know about buffer overflows, null pointers, use-after-free and data races, and has to avoid each one by hand. The compiler does not help.
Rust makes ownership, borrowing, lifetimes and thread safety part of the type system, so the compiler checks them in every program. None of these ideas is new; Rust’s contribution is checking all of them in the types of an everyday systems language.
For runtime engineering, the gain is that thread safety becomes part of the types: a value that is not safe to share cannot be shared by accident. It does not remove an interpreter’s rooting problem. The rrs interpreter, written in safe Rust, still keeps a protect stack for its own collector, because R objects refer to each other through handles that the borrow checker cannot see.
Rust for runtime engineering
An R runtime is complex. It has a heap, a call stack, an environment chain, a symbol table. Threads share some of these. Interpreter instances own others. Mutations happen deep in evaluation.
Rust forces this complexity into the type system. Who owns the symbol table, who may mutate it, and whether it may be shared across threads are all written in the types, and the compiler rejects code that disagrees.
This section covers the Rust foundations an interpreter needs. Traits for shared behavior, memory layout and the repr attribute are outside its scope.