Evaluate Expressions Only

Tip: Dynamic

Use Dynamic if you’re uncertain of the return type.

Very often, 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.

let result: i64 = engine.eval_expression("2 + (10 + 10) * 2")?; let result: Dynamic = engine.eval_expression("get_value(42)")?; // Usually this is done together with a custom scope with variables... let mut scope = Scope::new(); scope.push("x", 42_i64); scope.push_constant("SCALE", 10_i64); let result: i64 = engine.eval_expression_with_scope(&mut scope, "(x + 1) * SCALE" )?;

No statements allowed

When evaluating expressions, no full-blown statement (e.g. while, for, fn) – not even variable assignment – is supported and will be considered syntax errors.

This is true also for statement expressions and closures.

// The following are all syntax errors because the script // is not a strict expression. engine.eval_expression::<()>("x = 42")?; let ast = engine.compile_expression("let x = 42")?; let result = engine.eval_expression_with_scope::<i64>(&mut scope, "{ let y = calc(x); x + y }" )?; let fp: FnPtr = engine.eval_expression("|x| x + 1")?;

Tip: if-expressions and switch-expressions

if expressions are allowed if both statement blocks contain only a single expression each.

switch expressions are allowed if all match actions are expressions and not statements.

loop expressions are not allowed.

// The following are allowed. let result = engine.eval_expression_with_scope::<i64>(&mut scope, "if x { 42 } else { 123 }" )?; let result = engine.eval_expression_with_scope::<i64>(&mut scope, " switch x { 0 => x * 42, 1..=9 => foo(123) + bar(1), 10 => 0, } ")?;