safe_eval_with_stack

Function safe_eval_with_stack 

Source
pub fn safe_eval_with_stack(
    input: &str,
    initial_stack: VecDeque<f64>,
) -> Result<VecDeque<f64>, EvaluationError>
Expand description

safe_eval_with_stack takes an &str expression, and a stack VecDeque<f64>, and evaluates the expression, returning the resulting stack as a Result if the if the expression is valid, or an Err if the expression is invalid or otherwise cannot be evaluated.

safe_eval_with_stack is useful for testing the validity of an expression, and is safer than eval, as it does not mutate any inputted values.

It also allows you to specify a stack to use for the expression, rather than automatically creating a new stack internally. This is useful for persisting a stack between calls to safe_eval_with_stack.

ยงExamples

use dc-ock::safe_eval_with_stack;

fn main() {
    let mut stack: VecDeque<f64> = VecDeque::new();
    stack.push_back(2.);
    stack.push_back(7.5);
    stack.push_back(3.5);

    stack = safe_eval_with_stack("+ +", stack).unwrap();
    println!("{:?}", stack); // prints [13.0]
}