my-little-eval

Crates.iomy-little-eval
lib.rsmy-little-eval
version0.1.1
sourcesrc
created_at2023-07-04 14:48:05.762565
updated_at2023-07-04 17:24:47.221723
descriptionA little library for evaluating mathematical expressions
homepage
repositoryhttps://github.com/JonasSeifried/rust-calculator
max_upload_size
id908038
size32,213
Jonas Seifried (JonasSeifried)

documentation

https://docs.rs/my-little-eval

README

crates.io docs.rs

My Little Eval

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.

Features

  • Parsing and evaluation of mathematical expressions
  • Supported Types integer (i32), float (f64) and strings (String)
  • Support for basic arithmetic operations (+, -, *, /, %)
  • Parentheses for controlling operator precedence
  • Variable substitution

Supported Operations

Type Type + - * / %
Int Int ✔️ ✔️ ✔️ ✔️ ✔️
Int Float ✔️ ✔️ ✔️ ✔️ ✔️
Int String ✔️ ✔️
Float Float ✔️ ✔️ ✔️ ✔️ ✔️
Float Int ✔️ ✔️ ✔️ ✔️ ✔️
Float String ✔️
String String ✔️
String Int ✔️ ✔️
String Float ✔️

Installation

Add the following line to your Cargo.toml file

[dependencies]
my-little-eval = "0.1.0"

or run

cargo add my-little-eval

Examples

Lookup the 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")));

Git Clone or Fork

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
>>>

Examples

>>>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
Commit count: 33

cargo fmt