3.1 rustinr R package

rustinr is a R package to help user generate the basic struture of a Rust-R package and source Rust script in R. Install it with

devtools::install_github("rustr/rustinr")

3.1.1 Rust Function in R Console

rust() is a R function to create Rust function in R console interatively.

For a Rust function:

pub fn say_hi() -> String{
    "Hello World".into()
}

Just mark it with // #[rustr_export], and put this function in rust()

library(rustinr)

rust(code = '
// #[rustr_export]
pub fn say_hi() -> String{
    "Hello World".into()
}
')

say_hi()
#> [1] "Hello World"

and then you can call say_hi in R.

say_hi
#> function () 
#> {
#>     .Call("ouNIkssJBKBM_say_hi", PACKAGE = "ouNIkssJBKBM")
#> }

Here is another example:

rust(code = '
// #[rustr_export]
pub fn fib_rs(x:u64)-> u64{
    if x == 0 { return 0 };
    if x == 1 { return 1 };
    return fib_rs(x - 1) + fib_rs(x - 2);
}')

fib_rs(10L)
#> [1] 55

For more example, you can also checkout https://gallery.rustr.org