5 个版本
| 0.1.4 | 2023年12月5日 |
|---|---|
| 0.1.3 | 2023年11月24日 |
| 0.1.2 | 2023年11月22日 |
| 0.1.1 | 2023年11月21日 |
| 0.1.0 | 2023年11月21日 |
#736 in 数学
61KB
1K SLoC
m_calc
!!! 此 crate 正在开发中,未来的更新将包括破坏性更改 !!!
m_calc 是一个用于在字符串中计算数学表达式的 Rust crate。
例如,字符串 "2*3^(4-2)+14%3" 应计算为 20
它还支持数学函数,如:sqrt(3.14159) 约等于 0
以及变量:"a=10;5+a" -> 15
用法
添加到 cargo 项目
cargo add m_calc
将字符串解析为标记向量,并计算标记向量。
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());
依赖项
~2.8–4MB
~69K SLoC