example: add calc example from readme

This commit is contained in:
Dmitriy Pleshevskiy 2022-07-24 02:23:19 +03:00
parent 3f6c144e8d
commit f8c97b6965
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
2 changed files with 19 additions and 0 deletions

View File

@ -30,3 +30,7 @@ vec = []
[badges]
maintenance = { status = "actively-developed" }
[[example]]
name = "calc"
required-features = ["vec", "number"]

15
examples/calc.rs Normal file
View File

@ -0,0 +1,15 @@
use estring::{EString, SepVec};
type PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>;
fn main() -> Result<(), estring::ParseError> {
let res = EString::from("10+5*2+3")
.parse::<PlusVec<MulVec<f32>>>()?
.iter()
.map(|m| m.iter().product::<f32>())
.sum::<f32>();
assert_eq!(res, 23.0);
Ok(())
}