| Crates.io | evalx |
| lib.rs | evalx |
| version | 0.5.0 |
| created_at | 2025-09-23 17:12:52.609275+00 |
| updated_at | 2025-09-23 17:12:52.609275+00 |
| description | Expression evaluator |
| homepage | https://github.com/nlippke/evalx |
| repository | https://github.com/nlippke/evalx |
| max_upload_size | |
| id | 1851778 |
| size | 80,324 |
Evalx is a powerful expression evaluator.
This crate is a maintained fork and further development of the original eval by fengcen. The continuation mainly includes bugfixes and updating the Rust edition to 2021, while keeping the public API compatible.
Supported operators: ! != "" '' () [] , > < >= <= ==
+ - * / % && || n..m.
Built-in functions: min() max() len() is_empty() array().
Add dependency to Cargo.toml
[dependencies]
evalx = "^0.5"
In your code:
use evalx::{eval, Expr, to_value};
You can do mathematical calculations with supported operators:
use evalx::{eval, to_value};
assert_eq!(eval("1 + 2 + 3"), Ok(to_value(6)));
assert_eq!(eval("2 * 2 + 3"), Ok(to_value(7)));
assert_eq!(eval("2 / 2 + 3 / 3"), Ok(to_value(2.0)));
You can eval with context:
use evalx::{Expr, to_value};
assert_eq!(Expr::new("foo == bar")
.value("foo", true)
.value("bar", true)
.exec(),
Ok(to_value(true)));
You can access data like javascript by using . and []. [] supports expression.
use evalx::{Expr, to_value};
use std::collections::HashMap;
let mut object = HashMap::new();
object.insert("foos", vec!["Hello", "world", "!"]);
assert_eq!(Expr::new("object.foos[1-1] == 'Hello'")
.value("object", object)
.exec(),
Ok(to_value(true)));
You can eval with function:
use evalx::{Expr, to_value};
assert_eq!(Expr::new("say_hello()")
.function("say_hello", |_| Ok(to_value("Hello world!")))
.exec(),
Ok(to_value("Hello world!")));
You can create an array with array():
use evalx::{eval, to_value};
assert_eq!(eval("array(1, 2, 3, 4, 5)"), Ok(to_value(vec![1, 2, 3, 4, 5])));
You can create an integer array with n..m:
use evalx::{eval, to_value};
assert_eq!(eval("0..5"), Ok(to_value(vec![0, 1, 2, 3, 4])));
This repository is set up with Clippy for linting.
rustup component add clippycargo clippycargo clippy-strictcargo clippy-pedantic.github/workflows/ci.yml).evalx is primarily distributed under the terms of the MIT license. See LICENSE for details.