rhai/doc/src/engine/compile.md
2020-12-16 14:57:28 +08:00

679 B

Compile a Script (to AST)

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

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.

// 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):

let ast = engine.compile_file("hello_world.rhai".into())?;