pub struct Expr { /* private fields */ }Expand description
A parsed and optimized mathematical expression.
§Examples
let expr = Expr::parse("3 + 5 * 2").unwrap();
assert_eq!(expr.eval(None), Ok(13.0));
let mut context :HashMap<String,f64> = HashMap::new();
context.insert("a".into(), 42.0);
let expr = Expr::parse("-2 * a").unwrap();
assert_eq!(expr.eval(&context), Ok(-84.0));Implementations§
Source§impl Expr
impl Expr
Sourcepub fn parse(expression: &str) -> Result<Self, Error>
pub fn parse(expression: &str) -> Result<Self, Error>
Parse the given mathematical expression into an Expr.
§Examples
// A valid expression
assert!(Expr::parse("3 + 5 * 2").is_ok());
// an invalid expression
assert!(Expr::parse("3eff + 5 * 2").is_err());Sourcepub fn eval<'a, C>(&self, context: C) -> Result<f64, Error>
pub fn eval<'a, C>(&self, context: C) -> Result<f64, Error>
Evaluate the expression in a given optional context.
§Examples
let expr = Expr::parse("3 + 5 * 2").unwrap();
assert_eq!(expr.eval(None), Ok(13.0));
let expr = Expr::parse("3 + a").unwrap();
let mut context :HashMap<String,f64> = HashMap::new();
context.insert("a".into(), -5.0);
assert_eq!(expr.eval(&context), Ok(-2.0));
context.insert("a".into(), 2.0);
assert_eq!(expr.eval(&context), Ok(5.0));Trait Implementations§
impl StructuralPartialEq for Expr
Auto Trait Implementations§
impl Freeze for Expr
impl RefUnwindSafe for Expr
impl Send for Expr
impl Sync for Expr
impl Unpin for Expr
impl UnwindSafe for Expr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more