Struct meval::Context [] [src]

pub struct Context<'a> { /* fields omitted */ }

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

Example

use meval::{eval_str_with_context, Context};

let mut ctx = Context::new(); // builtins
ctx.var("x", 3.)
   .func("f", |x| 2. * x)
   .funcn("sum", |xs| xs.iter().sum(), ..);

assert_eq!(eval_str_with_context("pi + sum(1., 2.) + f(x)", &ctx),
           Ok(std::f64::consts::PI + 1. + 2. + 2. * 3.));

Methods

impl<'a> Context<'a>
[src]

Creates a context with built-in constants and functions.

Creates an empty contexts.

Adds a new variable/constant.

Adds a new function of one argument.

Adds a new function of two arguments.

Adds a new function of three arguments.

Adds a new function of a variable number of arguments.

n_args specifies the allowed number of variables by giving an exact number n or a range n..m, .., n.., ..m. The range is half-open, exclusive on the right, as is common in Rust standard library.

Example

let mut ctx = meval::Context::empty();

// require exactly 2 arguments
ctx.funcn("sum_two", |xs| xs[0] + xs[1], 2);

// allow an arbitrary number of arguments
ctx.funcn("sum", |xs| xs.iter().sum(), ..);

Trait Implementations

impl<'a> ContextProvider for Context<'a>
[src]