/hellorust

Template R package with rust bindings

Primary LanguageRustOtherNOASSERTION

Hello Rust

Build Status AppVeyor Build Status

Minimal Example of Calling Rust from R using Cargo

Rust is a modern alternative to C and compiled rust code is ABI compatible with C. Many Rust libraries include C API headers so that the compiled rust code can be called from R/C/C++ as if it were C code.

The r-rust organization contains several example R packages interfacing with Rust. Also have a look at the slides about this project presented at eRum2018!

Cargo

The standard rust toolchain includes a great package manager cargo with a corresponding registry crates.io. Cargo makes it very easy to build a rust package including all dependencies into a static library that can easily be linked into an R package.

This is perfect for R because we can compile and link all rust code at build-time without any system dependencies. Rust itself has no substantial runtime so the resulting R package is entirely self contained. Indeed, rust has been designed specifically to serve well as an embedded language.

Package Structure

Simply bundle your rust code into a cargo package (just add a Cargo.toml file) and then invoke the build + link as shown in src/Makevars.

hellorust
├─ configure            ← checks if 'cargo' is installed
├─ src
│  ├─ myrustlib            ← bundled cargo package with your code
│  |  ├─ Cargo.toml          ← cargo dependencies and metadata
│  |  ├─ src                 ← rust source code
│  |  └─ api.h               ← C headers for exported rust API
|  |
│  ├─ Makevars          ← Ties everything together
│  └─ wrapper.c         ← C code for R package
├─ DESCRIPTION
└─ R                    ← Standard R+C stuff

Installation

Note that cargo is only needed at build-time. Rust has no runtime dependencies. To install on MacOS use homebrew:

brew install rust

And on Debian/Ubuntu:

sudo apt-get install cargo

And on Fedora / CentOS:

sudo yum install cargo

And on Arch:

sudo pacman -Sy cargo

On CentOS you first need to enable EPEL via sudo yum install epel-release.

Windows

In order for rust to work with R you need to install the toolchain using rustup and then add the x86_64-pc-windows-gnu and i686-pc-windows-gnu targets. First download rustup-init.exe and then install the default toolchain:

rustup-init.exe -y --default-host x86_64-pc-windows-gnu

To compile 32bit packages also add the i686 target:

rustup target add i686-pc-windows-gnu

The appveyor.yml file shows this live in action. For more information about rust on Windows see here.

Resources