Crate meval [] [src]

This Rust crate provides a simple math expression parsing and evaluation. Its main goal is to be convenient to use, while allowing for some flexibility. Currently works only with f64 types. A typical use case is the configuration of numerical computations in Rust, think initial data and boundary conditions, via config files or command line arguments.



Simply add the corresponding entry to your Cargo.toml dependency list:

meval = "0.0.7"

and add this to your crate root:

extern crate meval;

Simple examples

extern crate meval;

fn main() {
    let r = meval::eval_str("1 + 2").unwrap();

    println!("1 + 2 = {}", r);

Need to define a Rust function from an expression? No problem, use Expr for this and more:

extern crate meval;

fn main() {
    let expr = meval::Expr::from_str("sin(pi * x)").unwrap();
    let func = expr.bind("x").unwrap();

    let vs: Vec<_> = (0..100+1).map(|i| func(i as f64 / 100.)).collect();

    println!("sin(pi * x), 0 <= x <= 1: {:?}", vs);

Expr::bind returns a boxed closure that is slightly less convenient than an unboxed closure since Box<Fn(f64) -> f64> does not implement FnOnce, Fn or FnMut. So to use it directly as a function argument where a closure is expected, it has to be manually dereferenced:

let func = meval::Expr::from_str("x").unwrap().bind("x").unwrap();
let r = Some(2.).map(&*func);

Custom constants and functions? Define a Context!

use meval::{Expr, Context};

let y = 1.;
let expr = Expr::from_str("phi(-2 * zeta + x)").unwrap();

// create a context with function definitions and variables
let mut ctx = Context::new(); // built-ins
ctx.func("phi", |x| x + y)
   .var("zeta", -1.);
// bind function with a custom context
let func = expr.bind_with_context(ctx, "x").unwrap();
assert_eq!(func(2.), -2. * -1. + 2. + 1.);

For functions of 2, 3, and N variables use Context::func2, Context::func3 and Context::funcn, respectively. See Context for more options.

If you need a custom function depending on mutable parameters, you will need to use a Cell:

use std::cell::Cell;
use meval::{Expr, Context};
let y = Cell::new(0.);
let expr = Expr::from_str("phi(x)").unwrap();

let mut ctx = Context::empty(); // no built-ins
ctx.func("phi", |x| x + y.get());

let func = expr.bind_with_context(ctx, "x").unwrap();
assert_eq!(func(2.), 2.);
assert_eq!(func(2.), 5.);

Supported expressions

meval supports basic mathematical operations on floating point numbers:

  • binary operators: +, -, *, /, % (remainder), ^ (power)
  • unary operators: +, -

It supports custom variables and functions like x, weight, C_0, f(1), etc. A variable or function name must start with [a-zA-Z_] and can contain only [a-zA-Z0-9_]. Custom functions with a variable number of arguments are also supported.

Build-ins (given by the context Context::new() and when no context provided) currently supported:

  • functions implemented using functions of the same name in Rust std library:

    • sqrt, abs
    • exp, ln
    • sin, cos, tan, asin, acos, atan, atan2
    • sinh, cosh, tanh, asinh, acosh, atanh
    • floor, ceil, round
    • signum
  • other functions:

    • max(x, ...), min(x, ...): maximum and minimumum of 1 or more numbers
  • constants:

    • pi
    • e

Related projects

This is a toy project of mine for learning Rust, and to be hopefully useful when writing command line scripts. For other similar projects see:


pub use shunting_yard::RPNError;
pub use tokenizer::ParseError;



Implementation of the shunting-yard algorithm for converting an infix expression to an expression in reverse Polish notation (RPN).


Tokenizer that converts a mathematical expression in a string form into a series of Tokens.



A structure for storing variables/constants and functions to be used in an expression.


Representation of a parsed expression.



An error produced during parsing or evaluation.


Function evaluation error.



Trait for types that can specify the number of required arguments for a function with a variable number of arguments.


A trait of a source of variables (and constants) and functions for substitution into an evaluated expression.



Evaluates a string with built-in constants and functions.


Evaluates a string with the given context.