Compile a Script (to AST)

To repeatedly evaluate a script, compile it first with Engine::compile into an AST (abstract syntax tree) form.

Engine::eval_ast evaluates a pre-compiled AST.


#![allow(unused)]
fn main() {
// Compile to an AST and store it for later evaluations
let ast = engine.compile("40 + 2")?;

for _ in 0..42 {
    let result: i64 = engine.eval_ast(&ast)?;

    println!("Answer #{}: {}", i, result);      // prints 42
}
}

Compiling a script file is also supported with Engine::compile_file (not available under no_std or in WASM builds):


#![allow(unused)]
fn main() {
let ast = engine.compile_file("hello_world.rhai".into())?;
}

Unix Shebangs

On Unix-like systems, the shebang (#!) is used at the very beginning of a script file to mark a script with an interpreter (for Rhai this would be rhai-run).

If a script file starts with #!, the entire first line is skipped by Engine::compile_file and Engine::eval_file. Because of this, Rhai scripts with shebangs at the beginning need no special processing.

#!/home/to/me/bin/rhai-run

// This is a Rhai script

let answer = 42;
print(`The answer is: ${answer}`);

AST Manipulation API

Advanced users may want to manipulate an AST, especially the functions contained within.

See the section on Manage AST’s for more details.