run_rs_step: Execute a Rust step (worker behind tar_target_rs)

View source: R/run-step-rs.R

run_rs_stepR Documentation

Execute a Rust step (worker behind tar_target_rs)

Description

Compiles the ⁠#[extendr]⁠ functions in a Rust script with rextendr::rust_source(), exposing them as R functions in a fresh environment, then evaluates an R post-script in that environment where you call those functions and return the result. Upstream inputs are bound in the same environment. This is the function the target built by tar_target_rs() calls; it is exported so the call resolves at run time, but package users should not call it directly.

Usage

run_rs_step(
  script,
  post_script = NULL,
  inputs = list(),
  output = "object",
  dependencies = NULL,
  features = NULL,
  profile = NULL,
  toolchain = NULL
)

Arguments

script

Path to the Rust script containing ⁠#[extendr]⁠ functions (required). Accepts a file path or an inline tar_code() carrier; see the "Script arguments" section below.

post_script

Path to an R script evaluated after compilation. The compiled Rust functions and the named inputs are in scope; its last expression is the target value (object mode), or it returns a character vector of file paths (file mode). Required for object mode. Accepts a file path or an inline tar_code() carrier; see the "Script arguments" section below.

inputs

Named character vector (or list) mapping the name seen inside the step (in the R environment and, after the hand-off, in the foreign session) to the name of an upstream target, e.g. c(x = "prepared_x"). Each upstream target becomes a dependency of this target and is bound by that name in the step environment; under dynamic branching the per-branch slice is bound instead.

output

Output mode: "object" (default) returns a converted R object, "file" returns a character vector of file paths (and defaults format to "file").

dependencies, features, profile

Passed to rextendr::rust_source(): crate dependencies (named list), Cargo features, and build profile (e.g. "dev" or "release").

toolchain

Optional rustup toolchain (e.g. "stable-x86_64-pc-windows-gnu"); sets RUSTUP_TOOLCHAIN for the build. Default NULL uses the rustup default toolchain.

Details

Unlike Python/Julia there is no pre-script for Rust and no live interpreter: rust_source() compiles a dynamic library and R calls into it with real type conversion (via extendr). A Rust toolchain and cargo must be reachable (this function puts R, cargo, and on Windows Rtools on PATH for the build itself); on Windows use the GNU toolchain.

Value

The value of the post-script (object mode) or a character vector of normalised file paths (file mode).

Script arguments

A worker receives whatever the constructor already resolved, which is one of two things: a path to a file on disk, or an inline carrier built by tar_code() that holds the code in memory. Both are accepted, so a direct call may pass either.

tar_target_path() is deliberately not a third form at this level. It is a constructor-level convenience: tar_target_py() and the other constructors rewrite it while the pipeline's DAG is built, so that by the time a worker runs it has already become the ordinary file path held by the upstream target. Handing the result of tar_target_path() straight to a worker therefore does not resolve to a file. The three forms as written in ⁠_targets.R⁠, and which of them tracks your edits, are covered in vignette("scripts").

See Also

tar_target_rs(), run_py_step(), run_jl_step()

Examples

# This worker compiles Rust with cargo, so it only runs when
# TARPOLYGLOT_EXAMPLES=true says a Rust toolchain is available.
# tar_dir() runs the code in a temporary directory.
if (identical(Sys.getenv("TARPOLYGLOT_EXAMPLES"), "true")) {
  targets::tar_dir({
    # Normally invoked by tar_target_rs(); shown here as a direct call.
    writeLines("#[extendr] fn square(x: f64) -> f64 { x * x }", "square.rs")
    writeLines("square(x)", "post.R")
    run_rs_step(
      script = "square.rs",
      inputs = list(x = 21),
      post_script = "post.R"
    )
  })
}

tarpolyglot documentation built on Nov. 22, 2026, 5:07 p.m.