Merge pull request #8 from pleshevskiy/examples

example: add calc example from readme
This commit is contained in:
Dmitriy Pleshevskiy 2022-07-23 23:24:00 +00:00 committed by GitHub
commit 372f8af96c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View file

@ -30,3 +30,7 @@ vec = []
[badges] [badges]
maintenance = { status = "actively-developed" } 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(())
}