Domain-Specific Tools

Usage scenario

Key concepts

  • Leverage the pre-packaged bin tools – it is easy because each of them is one single source file.

  • Modify the Engine creation code to include domain-specific registrations.


Copy necessary tool source files

Each bin tool is a single source file.

Download the necessary one(s) into the project’s bin or example subdirectory.

Select this source fileTo make
rhai-run.rsa simple script runner for the system
rhai-repl.rsan interactive REPL for the system
rhai-dbg.rsa script debugger for the system

Example -> /path/to/my_project/examples/ -> /path/to/my_project/examples/ -> /path/to/my_project/examples/

Leverage Engine configuration code in the project

Assume the project already contains configuration code for a customized Engine.

use rhai::Engine;
use rhai::plugin::*;

// Domain-specific data types
use my_project::*;

mod my_domain_api {
    // plugin module

// This function creates a new Rhai scripting engine and
// configures it properly
fn create_scripting_engine(config: MySystemConfig) -> Engine {
    let mut engine = Engine::new();

    // Register domain-specific API into the Engine
        // register API functions

    // Plugin modules can be used to easily and quickly
    // register an entire API

    // Configuration options in 'MySystemConfig' may be used
    // to control the Engine's behavior
    if config.strict_mode {

    // Return the scripting engine

Modify Engine creation

Each bin tool contains a line that creates the main script Engine.

Modify it to call the project’s creation function.

// Initialize scripting engine
let mut engine = Engine::new();

// Modify to this:
let mut engine = create_scripting_engine(my_config);

Make sure that Rhai has the appropriate feature(s)

In the project’s Cargo.toml, specify the Rhai dependency with bin-features.

rhai = { version = "1.19.0", features = ["bin-features"] }


Rebuild the project, which should automatically build all the customized tools.

Run tools

Each customized tool now has access to the entire domain-specific API. Functions and custom types can be used in REPL, debugging etc.