bosscar
bosscar

Reputation: 1

Evaluate string expression in rust with conditionals

I am trying to evaluate a string in rust with conditionals using evalexpr module. This results in the following error

thread 'main' panicked at 'called Result::unwrap() on an Err value: AppendedToLeafNode', src\main.rs:6:56

The error description says https://docs.rs/evalexpr/5.0.5/evalexpr/error/enum.EvalexprError.html#variant.AppendedToLeafNode

Tried to append a child to a leaf node. Leaf nodes cannot have children.

I am not able to understand why this error is happening. Does anyone have a clue? Is it possible to evaluate an string with conditionals in Rust?

use evalexpr::*;
fn main() {
    let evalstr = "let x = if input == 8 { 3 } else { 4 };";
    let mut context = HashMapContext::new();
    context.set_value("input".into(), 8.into()).unwrap();
    eval_empty_with_context_mut(evalstr, &mut context).unwrap();
    let val = context.get_value("x").unwrap();
    println!("{}", val);
}

Upvotes: 0

Views: 1167

Answers (2)

Sunding Wei
Sunding Wei

Reputation: 2224

Today, the rust evalexpr has a built-in if function: if(condition, a, b)

Upvotes: 1

Daniel
Daniel

Reputation: 16464

evalexpr doesn't support the Rust language, but uses its own little language documented here: https://docs.rs/evalexpr/6.6.0/evalexpr/index.html#features

That little language does not support conditionals yet: https://github.com/ISibboI/evalexpr/issues/82

Upvotes: 3

Related Questions