Evaluate Expressions Only
Sometimes a use case does not require a full-blown scripting language, but only needs to evaluate expressions.
In these cases, use the Engine::compile_expression
and Engine::eval_expression
methods or their _with_scope
variants.
#![allow(unused)] fn main() { let result = engine.eval_expression::<i64>("2 + (10 + 10) * 2")?; }
When evaluating expressions, no full-blown statement (e.g. if
, while
, for
, fn
) – not even variable assignment –
is supported and will be considered parse errors when encountered.
Closures and anonymous functions are also not supported because in the background they compile to functions.
#![allow(unused)] fn main() { // The following are all syntax errors because the script is not an expression. engine.eval_expression::<()>("x = 42")?; let ast = engine.compile_expression("let x = 42")?; let result = engine.eval_expression_with_scope::<i64>(&mut scope, "if x { 42 } else { 123 }")?; }