[−][src]Struct cruncher::expr::Expr
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));
Methods
impl Expr
[src]
pub fn parse(expression: &str) -> Result<Self, Error>
[src]
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());
pub fn eval<'a, C>(&self, context: C) -> Result<f64, Error> where
C: Into<Option<&'a HashMap<String, f64>>>,
[src]
C: Into<Option<&'a HashMap<String, f64>>>,
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 Clone for Expr
[src]
fn clone(&self) -> Expr
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl PartialEq<Expr> for Expr
[src]
impl Debug for Expr
[src]
Auto Trait Implementations
impl Sync for Expr
impl Send for Expr
impl Unpin for Expr
impl RefUnwindSafe for Expr
impl UnwindSafe for Expr
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,