My Little Eval is a lightweight Rust library for evaluating mathematical expressions. It provides a simple and intuitive way to parse and calculate mathematical expressions in your Rust programs.
i32
), float (f64
) and strings (String
)+
, -
, *
, /
, %
)Variable
substitution | Type | Type | + | - | * | / | % |
|---------|--------|---|---|---|---|---|
| Int | Int |✔️ |✔️ |✔️|✔️ |✔️|
| Int | Float |✔️ |✔️ |✔️|✔️ |✔️|
| Int | String |✔️ |❌ |✔️|❌ |❌|
| Float | Float | ✔️|✔️ |✔️|✔️ |✔️|
| Float | Int | ✔️|✔️ |✔️|✔️ |✔️|
| Float | String | ✔️|❌ |❌|❌ |❌|
| String | String | ✔️|❌ |❌|❌ |❌|
| String | Int | ✔️|❌ |✔️|❌ |❌|
| String | Float | ✔️|❌ |❌|❌ |❌|
Add the following line to your Cargo.toml
file
toml
[dependencies]
my-little-eval = "0.1.0"
or run
sh
cargo add my-little-eval
Documentation
for more infos!```rust use std::collections::HashMap; use mylittleeval::{eval, varsinit, typeenum::Type};
let mut variables = vars_init();
// Inserting variables into the HashMap variables.insert("x".tostring(), Type::Int(42)); variables.insert("y".tostring(), Type::Float(3.14));
// Using the eval
function to evaluate an equation
let equation = "2 * x + y";
let result = eval(equation, Some(&variables));
assert_eq!(result, Ok(Type::from("87.14"))); ```
If u clone or fork this git u can test the library with the included main.rs file. It contains a little CLI Shell wrapper around it.
sh
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target\debug\my-little-eval.exe`
run help for instructions
Run help for instructions
```sh
help help page: Define a variable with the let keyword eg. let hallo = 2 Evaluate a term eg. ( 1 + hallo ) * 2 Quit the program with command q || quit Print out all variables with command vars Print this help page
```
```rust
let a = 5
let b = 7 * 2
let c = a + b
c
res: i32 = 19 res * c
res: i32 = 361 (2 * (777 / 12))
res: i32 = 128 let hallo = hi
hallo * a
res: String = "hihihihihi" let foo = 7.5
let bar = 3.5
foo % bar
res: f64 = 0.5 ```