Rust Examples

Standard Examples

A number of examples can be found under examples.

ExampleDescription
arrays_and_structsshows how to register a Rust type and using arrays on it
custom_types_and_methodsshows how to register a Rust type and methods for it
hellosimple example that evaluates an expression and prints the result
reuse_scopeevaluates two pieces of code in separate runs, but using a common Scope
serdeexample to serialize and deserialize Rust types with serde (requires the serde feature)
simple_fnshows how to register a simple Rust function
stringsshows different ways to register Rust functions taking string arguments
threadingshows how to communicate with an Engine running in a separate thread via an MPSC channel

Scriptable Event Handler With State Examples

Because of its popularity, the pattern Scriptable Event Handler With State has sample implementations for different styles.

ExampleHandler ScriptDescription
event_handler_mainevent_handler_main/script.rhaiMain Style
event_handler_jsevent_handler_js/script.rhaiJS Style
event_handler_mapevent_handler_map/script.rhaiMap Style

Running Examples

Examples can be run with the following command:

cargo run --example {example_name}

no-std Examples

To illustrate no-std builds, a number of example applications are available under the no_std directory:

ExampleDescriptionOptimizationAllocatorPanics
no_std_testbare-bones test application that evaluates a Rhai expression and sets the result as the return valuesizewee_allocabort

Building the no-std examples

Currently, the nightly compiler must be used to build for no-std.

cd no_std/no_std_test

cargo +nightly build --release

./target/release/no_std_test