Struct meval::Context[][src]

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

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


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.));


impl<'a> Context<'a>

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.


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> Clone for Context<'a>

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<'a> Default for Context<'a>

Returns the "default value" for a type. Read more

impl<'a> ContextProvider for Context<'a>

Auto Trait Implementations

impl<'a> !Send for Context<'a>

impl<'a> !Sync for Context<'a>