Expand description
Simple arithmetic expression evaluator.
§Usage
To evaluate an expression, use the sari::eval
function:
use sari::Error;
let result = sari::eval("(1 + 2) * 3");
assert_eq!(result, Ok(9));
let result = sari::eval("(1 + 2");
assert_eq!(result, Err(Error::new("expected `)`")));
let result = sari::eval("1 / 0");
assert_eq!(result, Err(Error::new("division by zero")));
§Expressions
The expressions consist of integers combined using +
, -
, *
, and /
binary operators (with the usual precedence and associativity) and grouped
using parentheses. These elements can be separated by whitespace.
The expressions use wrapping 32-bit signed arithmetic. Division by zero is an error.
Structs§
- Error
- Error returned when expression evaluation fails.
Functions§
- eval
- Evaluates an expression and returns the result.