rhai/examples/reuse_scope.rs

23 lines
596 B
Rust
Raw Normal View History

2022-02-12 05:41:04 +01:00
//! An example that evaluates two pieces of code in separate runs, but using a common `Scope`.
use rhai::{Engine, EvalAltResult, Scope};
2016-03-04 14:45:43 +01:00
fn main() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2019-09-18 12:21:07 +02:00
let mut scope = Scope::new();
2016-03-04 14:45:43 +01:00
engine.run_with_scope(&mut scope, "let x = 4 + 5")?;
2016-03-04 14:45:43 +01:00
println!("x = {}", scope.get_value::<i64>("x").unwrap());
2020-03-09 09:54:43 +01:00
2021-02-20 16:46:25 +01:00
for _ in 0..10 {
let result = engine.eval_with_scope::<i64>(&mut scope, "x += 1; x")?;
2021-02-20 16:46:25 +01:00
println!("result: {}", result);
}
println!("x = {}", scope.get_value::<i64>("x").unwrap());
2020-03-09 09:54:43 +01:00
Ok(())
2016-03-04 14:45:43 +01:00
}