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]
fn new() -> Context<'a>
Creates a context with built-in constants and functions.
fn empty() -> Context<'a>
Creates an empty contexts.
fn var<S: Into<String>>(&mut self, var: S, value: f64) -> &mut Self
Adds a new variable/constant.
fn func<S, F>(&mut self, name: S, func: F) -> &mut Self where S: Into<String>, F: Fn(f64) -> f64 + 'a
Adds a new function of one argument.
fn func2<S, F>(&mut self, name: S, func: F) -> &mut Self where S: Into<String>, F: Fn(f64, f64) -> f64 + 'a
Adds a new function of two arguments.
fn func3<S, F>(&mut self, name: S, func: F) -> &mut Self where S: Into<String>, F: Fn(f64, f64, f64) -> f64 + 'a
Adds a new function of three arguments.
fn funcn<S, F, N>(&mut self, name: S, func: F, n_args: N) -> &mut Self where S: Into<String>, F: Fn(&[f64]) -> f64 + 'a, N: ArgGuard
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> Clone for Context<'a>
[src]
fn clone(&self) -> Context<'a>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more