rhai/doc/src/start/examples/rust.md
2020-09-24 11:17:39 +08:00

3.8 KiB

Rust Examples

{{#include ../../links.md}}

A number of examples can be found in the examples directory:

Example Description
arrays_and_structs shows how to register a custom Rust type and using [arrays] on it
custom_types_and_methods shows how to register a custom Rust type and methods for it
hello simple example that evaluates an expression and prints the result
reuse_scope evaluates two pieces of code in separate runs, but using a common [Scope]
rhai_runner runs each filename passed to it as a Rhai script
serde example to serialize and deserialize Rust types with serde.
The [serde] feature is required to run
simple_fn shows how to register a simple function
strings shows different ways to register functions taking string arguments
repl a simple REPL, interactively evaluate statements from stdin

The repl example is a particularly good one as it allows one to interactively try out Rhai's language features in a standard REPL (Read-Eval-Print Loop).

Running Examples

Examples can be run with the following command:

cargo run --example {example_name}

no-std Samples

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

Sample Description Optimization Allocator Panics
no_std_test bare-bones test application that evaluates a Rhai expression and sets the result as the return value size wee_alloc abort

cargo run cannot be used to run a no-std sample. It must first be built:

cd no_std/no_std_test

cargo +nightly build --release

./target/release/no_std_test