The Rhai Book
User’s Guide
Introduction
❱
Features of Rhai
Supported Targets and Builds
What Rhai Isn't
Licensing
Related Resources
Getting Started
❱
Online Playground
Install the Rhai Crate
Optional Features
Packaged Utilities
The Scripting Engine
❱
“Hello, Rhai”
Compile to AST
Raw Engine
❱
Built-in Operators
Scope – Maintaining State
Expressions Only
Options
Examples
❱
Rust
Scripts
Special Builds
❱
Performance
Minimal
no-std
WebAssembly (WASM)
Rust Integration
Introduction
Traits
Register a Rust Function
❱
String Parameters
Generic Functions
Fallible Functions
Dynamic Parameters
NativeCallContext
Override a Built-in Function
Call a Rhai Function from Rust
❱
Create a Rust Closure from a Rhai Function
Operator Overloading
Working with Any Rust Type
❱
Methods
Property Getters and Setters
Indexers
Call Method as Function
Collection Types
Disable Custom Types
Printing Custom Types
Modules
❱
Create in Rust
Create from AST
Use a Module
Module Resolvers
❱
Custom Module Resolvers
Self-Contained AST
Plugins
❱
Export a Rust Module
Export a Rust Function
Packages
❱
Built-in Packages
Create Custom Packages
Create Packages as Crates
External Packages
❱
Random Number Generation, Shuffling and Sampling
Scientific Computing
Scripting Language
Comments
❱
Doc-Comments
Values and Types
❱
Dynamic Values
❱
type_of()
Value Tag
Serialization/Deserialization with serde
Numbers
❱
Operators
Standard Functions
Value Conversions
Ranges
Bit-Fields
Strings and Characters
❱
ImmutableString
Standard Functions and Operators
Arrays
❱
BLOB's (Byte Arrays)
Object Maps
❱
Parse from JSON
Special Support for OOP
Timestamps
Keywords
Statements
❱
Statement Expression
Variables
❱
Variable Shadowing
Strict Variables Mode
Variable Definition Filter
Variable Resolver
Constants
❱
Automatic Global Module
Assignments
Compound Assignments
Logic Operators
In Operator
If Statement
❱
If Expression
Switch Statement
❱
Switch Expression
While Loop
Do Loop
Loop Statement
For Loop
❱
Make a Custom Type Iterable
Return Value
Throw Exception on Error
Catch Exceptions
Functions
❱
Method Calls
Overloading
Namespaces
Function Pointers
❱
Currying
Anonymous Functions
Closures
Metadata
❱
Get Scripted Functions Metadata in Rhai
Get Scripted Functions Metadata from AST
Get Native Function Signatures
Export All Functions Metadata to JSON
Print and Debug
Modules
❱
Export Variables, Functions and Sub-Modules from Script
Import Modules
Eval Function
Safety and Protection
Introduction
Sand-Boxing
Maximum Length of Strings
Maximum Size of Arrays
Maximum Size of Object Maps
Maximum Number of Operations
❱
Tracking Progress and Force-Termination
Maximum Number of Modules
Maximum Call Stack Depth
Maximum Expression Depth
Turn Off Safety Checks
Script Optimization
Introduction
Optimization Passes
❱
Dead Code Elimination
Constants Propagation
Compound Assignment Rewrite
Eager Operator Evaluation
Eager Function Evaluation
❱
Side-Effect Considerations
Volatility Considerations
Subtle Semantic Changes
Re-Optimize an AST
Advanced Topics
Manage AST's
Low-Level API to Register Functions
Evaluation Context
Call Function Within Caller's Scope
Use Rhai as a DSL
❱
Remap Tokens During Parsing
Disable Keywords and/or Operators
Disable Looping
Custom Operators
❱
Operator Precedence
Extend with Custom Syntax
Debugging Interface
❱
Debugger
❱
State
Call Stack
Break-Points
Debugging Server
Usage Patterns
Object-Oriented Programming (OOP)
Working with Rust Enums
Simulate Macros to Simplify Scripts
Dynamic Constants Provider
Global Constants
Mutable Global State
Loadable Configuration
Multi-Layered Functions
Hot Reloading
One Engine Instance Per Call
Multi-Threaded Synchronization
Control Layer Over Rust Backend
Singleton Command Object
Blocking/Async Function Calls
Scriptable Event Handler with State
❱
Main Style
JS Style
Map Style
Domain-Specific Tools
Multiple Instantiation
Language Reference
Introduction
Comments
Value Types
❱
Dynamic Values
❱
type_of()
Value Tag
Numbers
❱
Operators
Standard Functions
Value Conversions
Ranges
Bit-Fields
Strings and Characters
❱
Standard Functions and Operators
Arrays
❱
BLOB's (Byte Arrays)
Object Maps
❱
Special Support for OOP
Timestamps
Keywords
Statements
Variables
Constants
Assignments
Operators
Indexing
Properties
Methods
If Statement
Switch Statement
While Loop
Do Loop
Infinite Loop
For Loop
Return Value
Throw Exception on Error
❱
Catch Exceptions
Functions
❱
Method Calls
Overloading
Function Pointers
❱
Currying
Anonymous Functions
Closures
Metadata
Print and Debug
Modules
❱
Export Variables, Functions and Sub-Modules from Script
Import Modules
Eval Function
Appendix
External Tools
❱
Online Playground
Language Server
rhai-doc
Keywords
Operators and Symbols
Literals
Light (default)
Rust
Coal
Navy
Ayu
Rhai - Embedded Scripting for Rust
Rhai Language Reference
This is a stand-alone reference for the Rhai scripting language.