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 it with arrays
callbackshows how to store a Rhai closure and call it later within Rust
custom_types_and_methodsshows how to register a Rust type and methods/getters/setters for it
custom_typesshows how to register a Rust type and methods/getters/setters using the CustomType trait.
definitionsshows how to generate definition files for use with the Rhai Language Server (requires the metadata feature)
hellosimple example that evaluates an expression and prints the result
pause_and_resumeshows how to pause/resume/stop an Engine running in a separate thread via an MPSC channel
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 in duplex with an Engine running in a separate thread via a pair of MPSC channels

Scriptable Event Handler With State Examples

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

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

Nightly required

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