Register a Fallible Rust Function

If a function is fallible (i.e. it returns a Result<_, _>), it can be registered with via Engine::register_result_fn.

Return type

The function must return Result<T, Box<EvalAltResult>> where T is any clonable type.

use rhai::{Engine, EvalAltResult, Position};

// Function that may fail - the error type must be 'Box<EvalAltResult>'
fn safe_divide(x: i64, y: i64) -> Result<i64, Box<EvalAltResult>> {
    if y == 0 {
        // Return an error if y is zero
        Err("Division by zero!".into())         // shortcut to create Box<EvalAltResult::ErrorRuntime>
    } else {
        Ok(x / y)

let mut engine = Engine::new();

// Fallible functions that return Result values must use register_result_fn()
engine.register_result_fn("divide", safe_divide);

if let Err(error) = engine.eval::<i64>("divide(40, 0)") {
    println!("Error: {:?}", *error);         // prints ErrorRuntime("Division by zero detected!", (1, 1)")

Tip: Create a Box<EvalAltResult>

Box<EvalAltResult> implements From<&str> and From<String> etc. and the error text gets converted into Box<EvalAltResult::ErrorRuntime>.

The error values are Box-ed in order to reduce memory footprint of the error path, which should be hit rarely.