File: ns_callback.rs

package info (click to toggle)
rust-fasteval 0.2.4-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 952 kB
  • sloc: python: 14; makefile: 8
file content (18 lines) | stat: -rw-r--r-- 477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// usage:  cargo run --release --example ns_callback

fn main() -> Result<(), fasteval::Error> {
    let mut num_lookups = 0;
    let mut cb = |name:&str, _args:Vec<f64>| -> Option<f64> {
        num_lookups += 1;
        match name {
            "x" => Some(2.0),
            _ => None,
        }
    };

    let val = fasteval::ez_eval("x * (x + 1)", &mut cb)?;
    assert_eq!(val, 6.0);
    assert_eq!(num_lookups, 2);  // Notice that 'x' was looked-up twice.

    Ok(())
}