2022-02-12 05:41:04 +01:00
|
|
|
//! An example that evaluates two pieces of code in separate runs, but using a common `Scope`.
|
|
|
|
|
2022-01-11 15:12:46 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, Scope};
|
2016-03-04 14:45:43 +01:00
|
|
|
|
2020-04-21 17:25:12 +02:00
|
|
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
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
|
|
|
|
2022-01-11 15:12:46 +01:00
|
|
|
engine.run_with_scope(&mut scope, "let x = 4 + 5")?;
|
2016-03-04 14:45:43 +01:00
|
|
|
|
2022-01-11 15:12:46 +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 {
|
2022-01-11 15:12:46 +01:00
|
|
|
let result = engine.eval_with_scope::<i64>(&mut scope, "x += 1; x")?;
|
2021-02-20 16:46:25 +01:00
|
|
|
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("result: {result}");
|
2021-02-20 16:46:25 +01:00
|
|
|
}
|
|
|
|
|
2022-01-11 15:12:46 +01:00
|
|
|
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
|
|
|
}
|