Skip to main content

try_from_sexp

Function try_from_sexp 

Source
pub unsafe fn try_from_sexp<T>(x: SEXP) -> Result<T, T::Error>
where T: TryFromSexp,
Expand description

Convert an R SEXP to a Rust type, returning a Result.

Unlike from_sexp, this function returns a Result instead of calling rf_error on failure. Useful when you want custom error handling.

§Type Parameters

§Arguments

  • x - R value to convert

§Returns

Ok(T) on success, Err with the conversion error on failure.

§Safety

  • x must be a valid SEXP
  • Must be called on R’s main thread

§Example

match unsafe { try_from_sexp::<i32>(argv[0]) } {
    Ok(val) => { /* use val */ }
    Err(e) => {
        // Custom error handling
        rf_error(&format!("argument 1: {}", e));
    }
}