example: add calc example from readme
This commit is contained in:
parent
3f6c144e8d
commit
f8c97b6965
2 changed files with 19 additions and 0 deletions
|
@ -30,3 +30,7 @@ vec = []
|
|||
|
||||
[badges]
|
||||
maintenance = { status = "actively-developed" }
|
||||
|
||||
[[example]]
|
||||
name = "calc"
|
||||
required-features = ["vec", "number"]
|
||||
|
|
15
examples/calc.rs
Normal file
15
examples/calc.rs
Normal 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(())
|
||||
}
|
Reference in a new issue