Crates.io | my-little-eval |
lib.rs | my-little-eval |
version | 0.1.1 |
source | src |
created_at | 2023-07-04 14:48:05.762565 |
updated_at | 2023-07-04 17:24:47.221723 |
description | A little library for evaluating mathematical expressions |
homepage | |
repository | https://github.com/JonasSeifried/rust-calculator |
max_upload_size | |
id | 908038 |
size | 32,213 |
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
substitutionType | 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
[dependencies]
my-little-eval = "0.1.0"
or run
cargo add my-little-eval
Documentation
for more infos!use std::collections::HashMap;
use my_little_eval::{eval, vars_init, type_enum::Type};
let mut variables = vars_init();
// Inserting variables into the HashMap
variables.insert("x".to_string(), Type::Int(42));
variables.insert("y".to_string(), 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.
$ 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
>>>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
>>>
>>>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