rust_source() compiles and loads a single Rust file for use in R. rust_function() compiles and loads a single Rust function for use in R.

  code = NULL,
  module_name = "rextendr",
  dependencies = NULL,
  patch.crates_io = getOption("rextendr.patch.crates_io"),
  profile = c("dev", "release"),
  toolchain = getOption("rextendr.toolchain"),
  extendr_deps = getOption("rextendr.extendr_deps"),
  env = parent.frame(),
  use_extendr_api = TRUE,
  generate_module_macro = TRUE,
  cache_build = TRUE,
  quiet = FALSE,
  use_rtools = TRUE

rust_function(code, env = parent.frame(), ...)



Input rust file to source.


Input rust code, to be used instead of file.


Name of the module defined in the Rust source via extendr_module!. Default is "rextendr".


Character vector of dependencies lines to be added to the Cargo.toml file.


Character vector of patch statements for to be added to the Cargo.toml file.


Rust profile. Can be either "dev" or "release". The default, "dev", compiles faster but produces slower code.


Rust toolchain. The default, NULL, compiles with the system default toolchain. Accepts valid Rust toolchain qualifiers, such as "nightly", or (on Windows) "stable-msvc".


Versions of extendr-* crates. Defaults to list(`extendr-api` = "*").


The R environment in which the wrapping functions will be defined.


Logical indicating whether use extendr_api::prelude::*; should be added at the top of the Rust source provided via code. Default is TRUE. Ignored for Rust source provided via file.


Logical indicating whether the Rust module macro should be automatically generated from the code. Default is TRUE. Ignored for Rust source provided via file. The macro generation is done with make_module_macro() and it may fail in complex cases. If something doesn't work, try calling make_module_macro() on your code to see whether the generated macro code has issues.


Logical indicating whether builds should be cached between calls to rust_source().


Logical indicating whether compile output should be generated or not.


Logical indicating whether to append the path to Rtools to the PATH variable on Windows using the RTOOLS40_HOME environment variable (if it is set). The appended path depends on the process architecture. Does nothing on other platforms.


Other parameters handed off to rust_source().


The result from dyn.load(), which is an object of class DLLInfo. See getLoadedDLLs() for more details.


if (FALSE) { # creating a single rust function rust_function("fn add(a:f64, b:f64) -> f64 { a + b }") add(2.5, 4.7) # creating multiple rust functions at once code <- r"( #[extendr] fn hello() -> &'static str { "Hello, world!" } #[extendr] fn test( a: &str, b: i64) { rprintln!("Data sent to Rust: {}, {}", a, b); } )" rust_source(code = code) hello() test("a string", 42) # use case with an external dependency: a function that converts # markdown text to html, using the `pulldown_cmark` crate. code <- r"( use pulldown_cmark::{Parser, Options, html}; #[extendr] fn md_to_html(input: &str) -> String { let mut options = Options::empty(); options.insert(Options::ENABLE_TABLES); let parser = Parser::new_ext(input, options); let mut output = String::new(); html::push_html(&mut output, parser); output } )" rust_source( code = code, dependencies = list(`pulldown-cmark` = "0.8") ) md_text <- "# The story of the fox The quick brown fox **jumps over** the lazy dog. The quick *brown fox* jumps over the lazy dog." md_to_html(md_text) }