Expand description
§Rhai - embedded scripting for Rust
Rhai is a tiny, simple and fast embedded scripting language for Rust that gives you a safe and easy way to add scripting to your applications.
It provides a familiar syntax based on JavaScript+Rust and a simple Rust interface.
§A Quick Example
§Contents of my_script.rhai
/// Brute force factorial function
fn factorial(x) {
if x == 1 { return 1; }
x * factorial(x - 1)
}
// Calling an external function 'compute'
compute(factorial(10))§The Rust part
use rhai::{Engine, EvalAltResult};
fn main() -> Result<(), Box<EvalAltResult>>
{
// Define external function
fn compute_something(x: i64) -> bool {
(x % 40) == 0
}
// Create scripting engine
let mut engine = Engine::new();
// Register external function as 'compute'
engine.register_fn("compute", compute_something);
// Evaluate the script, expecting a 'bool' result
let result: bool = engine.eval_file("my_script.rhai".into())?;
assert_eq!(result, true);
Ok(())
}§Features
§On-Line Documentation
See The Rhai Book for details on the Rhai scripting engine and language.
Modules§
- config
- Configuration for Rhai.
- module_
resolvers - Module containing all built-in module resolvers.
- packages
- Module containing all built-in packages available to Rhai, plus facilities to define custom packages.
- plugin
- Module defining macros for developing plugins.
Macros§
- combine_
with_ exported_ module - Macro to combine a plugin module into an existing module.
- def_
package - Macro that makes it easy to define a package (which is basically a shared module) and register functions into it.
- exported_
module - Macro to generate a Rhai
Modulefrom a plugin module defined via#[export_module]. - register_
exported_ fn - Macro to register a plugin function (defined via
#[export_fn]) into anEngine. - set_
exported_ fn - Macro to register a plugin function into a Rhai
Module. - set_
exported_ global_ fn - Macro to register a plugin function into a Rhai
Moduleand expose it globally.
Structs§
- AST
- Compiled AST (abstract syntax tree) of a Rhai script.
- Call
FnOptions - Options for calling a script-defined function via
Engine::call_fn_with_options. - Dynamic
- Dynamic type containing any value.
- Engine
- Rhai main scripting engine.
- Eval
Context - Context of a script evaluation process.
- Expression
- An expression sub-tree in an
AST. - FnPtr
- A general function pointer, which may carry additional (i.e. curried) argument values to be passed onto a function during a call.
- Func
Registration - Type for fine-tuned module function registration.
- Immutable
String - The system immutable string type.
- Instant
- A measurement of a monotonically nondecreasing clock.
Opaque and useful only with
Duration. - Locked
- A reader-writer lock
- Module
- A module which may contain variables, sub-modules, external Rust functions, and/or script-defined functions.
- Native
Call Context - Context of a native Rust function call.
- Parse
Error - Error when parsing a script.
- Position
- A location (line number + character position) in the input script.
- Scope
- Type containing information about the current scope. Useful for keeping state between
Engineevaluation runs. - Script
FnMetadata - A type containing the metadata of a script-defined function.
- Shared
- A thread-safe reference-counting pointer. ‘Arc’ stands for ‘Atomically Reference Counted’.
- Type
Builder - Builder to build the API of a custom type for use with an
Engine. - VarDef
Info - Information on a variable declaration.
Enums§
- Eval
AltResult - Evaluation result.
- FnAccess
- A type representing the access mode of a function.
- FnNamespace
- A type representing the namespace of a function.
- LexError
- Error encountered when tokenizing the script text.
- Optimization
Level - Level of optimization performed.
- Parse
Error Type - Error encountered when parsing a script.
Constants§
- FUNC_
TO_ DEBUG - Standard debug-print function.
- FUNC_
TO_ STRING - Standard pretty-print function.
- OP_
CONTAINS - Standard containment testing function.
- OP_
EQUALS - Standard equality comparison operator.
Traits§
- Custom
Type - Trait to build the API of a custom type for use with an
Engine(i.e. register the type and its getters, setters, methods, etc.). - Func
- Trait to create a Rust closure from a script.
- Func
Args - Trait that parses arguments to a function call.
- Module
Resolver - Trait that encapsulates a module resolution service.
- Rhai
Native Func - Trait to register custom Rust functions.
Functions§
- eval
- Evaluate a string as a script, returning the result value or an error.
- eval_
file - Evaluate a script file, returning the result value or an error.
- format_
map_ as_ json - Return the JSON representation of an object map.
- run
- Evaluate a string as a script.
- run_
file - Evaluate a file.
Type Aliases§
- Array
- Variable-sized array of
Dynamicvalues. - Blob
- Variable-sized array of
u8values (byte array). - FLOAT
- The system floating-point type. It is defined as
f64. - INT
- The system integer type. It is defined as
i64. - Map
- A dictionary of
Dynamicvalues with string keys.
Attribute Macros§
- export_
fn - Attribute, when put on a Rust function, turns it into a plugin function.
- export_
module - Attribute, when put on a Rust module, turns it into a plugin module.
- expose_
under_ internals - Macro to automatically expose a Rust function, type-def or use statement as
pubwhen under theinternalsfeature.
Derive Macros§
- Custom
Type - Macro to implement the [
CustomType][rhai::CustomType] trait.