rhai/examples/reuse_scope.rs

21 lines
512 B
Rust
Raw Normal View History

2020-03-24 02:49:08 +01:00
use rhai::{Engine, EvalAltResult, Scope, INT};
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.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?;
2016-03-04 14:45:43 +01:00
2021-02-20 16:46:25 +01:00
println!("x = {}", scope.get_value::<INT>("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::<INT>(&mut scope, "x += 1; x")?;
println!("result: {}", result);
}
println!("x = {}", scope.get_value::<INT>("x").unwrap());
2020-03-09 09:54:43 +01:00
Ok(())
2016-03-04 14:45:43 +01:00
}