Eval is a powerful expression evaluator.
Supported operators: !
!=
""
''
()
[]
,
>
<
>=
<=
==
+
-
*
/
%
&&
||
n..m
.
Built-in functions: min()
max()
len()
is_empty()
.
Add dependency to Cargo.toml
toml
[dependencies]
eval = "^0.2"
In your main.rs
or lib.rs
:
rust
extern crate eval;
You can do mathematical calculations with supported operators:
``` use eval::{eval, to_value};
asserteq!(eval("1 + 2 + 3"), Ok(tovalue(6))); asserteq!(eval("2 * 2 + 3"), Ok(tovalue(7))); asserteq!(eval("2 / 2 + 3"), Ok(tovalue(4.0))); asserteq!(eval("2 / 2 + 3 / 3"), Ok(tovalue(2.0))); ```
You can eval with context:
``` use eval::{Expr, to_value};
asserteq!(Expr::new("foo == bar") .value("foo", true) .value("bar", true) .exec(), Ok(tovalue(true))); ```
You can eval with function:
``` use eval::{Expr, to_value};
asserteq!(Expr::new("sayhello()") .function("sayhello", || Ok(tovalue("Hello world!"))) .exec(), Ok(tovalue("Hello world!"))); ```
You can create an array with []
:
``` use eval::{eval, to_value};
asserteq!(eval("[1, 2, 3, 4, 5]"), Ok(tovalue(vec![1, 2, 3, 4, 5]))); ```
You can create an integer array with n..m
:
``` use eval::{eval, to_value};
asserteq!(eval("0..5"), Ok(tovalue(vec![0, 1, 2, 3, 4]))); ```
eval is primarily distributed under the terms of the MIT license. See LICENSE for details.