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]
impl<'a> Context<'a>
pub fn new() -> Context<'a>
[src]
pub fn new() -> Context<'a>
Creates a context with built-in constants and functions.
pub fn empty() -> Context<'a>
[src]
pub fn empty() -> Context<'a>
Creates an empty contexts.
pub fn var<S: Into<String>>(&mut self, var: S, value: f64) -> &mut Self
[src]
pub fn var<S: Into<String>>(&mut self, var: S, value: f64) -> &mut Self
Adds a new variable/constant.
pub fn func<S, F>(&mut self, name: S, func: F) -> &mut Self where
S: Into<String>,
F: Fn(f64) -> f64 + 'a,
[src]
pub 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.
pub fn func2<S, F>(&mut self, name: S, func: F) -> &mut Self where
S: Into<String>,
F: Fn(f64, f64) -> f64 + 'a,
[src]
pub 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.
pub fn func3<S, F>(&mut self, name: S, func: F) -> &mut Self where
S: Into<String>,
F: Fn(f64, f64, f64) -> f64 + 'a,
[src]
pub 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.
pub 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,
[src]
pub 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]
impl<'a> Clone for Context<'a>
fn clone(&self) -> 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[src]
fn clone_from(&mut self, source: &Self)
1.0.0
[src]Performs copy-assignment from source
. Read more
impl<'a> Default for Context<'a>
[src]
impl<'a> Default for Context<'a>
impl<'a> ContextProvider for Context<'a>
[src]
impl<'a> ContextProvider for Context<'a>