Struct caldyn::Expr[][src]

pub struct Expr { /* fields omitted */ }

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 = Context::new();
context.set("a", 42.0);
let expr = Expr::parse("-2 * a").unwrap();
assert_eq!(expr.eval(&context), Ok(-84.0));

Methods

impl Expr
[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());

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 = Context::new();
context.set("a", -5.0);
assert_eq!(expr.eval(&context), Ok(-2.0));
context.set("a", 2.0);
assert_eq!(expr.eval(&context), Ok(5.0));

Trait Implementations

impl Debug for Expr
[src]

Formats the value using the given formatter. Read more

impl Clone for Expr
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq for Expr
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

impl Send for Expr

impl Sync for Expr