Crates.io | m_calc |
lib.rs | m_calc |
version | 0.1.4 |
source | src |
created_at | 2023-11-21 06:36:07.733982 |
updated_at | 2023-12-05 20:19:33.07717 |
description | A library to calculate math expressions |
homepage | |
repository | https://github.com/Agent-E11/m_calc_rs |
max_upload_size | |
id | 1043764 |
size | 70,296 |
!!! this crate is under development and future updates will include breaking changes !!!
m_calc
is a Rust crate for calculating mathematical expressions in strings.
For instance, a string like "2*3^(4-2)+14%3"
should be calculated to 20
It also supports mathematical functions like: \sqrt(3.14159)
is approximately 0
And variables: "a=10;5+a"
-> 15
Add to cargo project
cargo add m_calc
Parse a string into a vector of tokens, and calculate the vector of tokens.
use m_calc::{tokenize, calculate};
use std::collections::HashMap;
let tokens = tokenize("2*3^(4-2)+14%3").unwrap();
let mut context = HashMap::new(); // The `context` is where variable assignments are stored
let res = calculate(&tokens, &mut context).unwrap();
println!("{}", res.display()); // `display` is a method of `Token`s that displays it as a simple expression (like `10`, or `a`)
assert_eq!("20", res.display());