From 23460c51fb641b654e7d0640ddd1b936fc662399 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 4 Mar 2016 08:27:06 -0500 Subject: [PATCH] Update README.md --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 115c4ea4..b2c6e4de 100644 --- a/README.md +++ b/README.md @@ -217,3 +217,25 @@ if let Ok(result) = engine.eval("var x = new_ts(); x.update(); x".to_string()).u println!("result: {}", result.x); // prints 1001 } ``` + +# Example: Maintaining state + +By default, Rhai treats each engine invocation as a fresh one, persisting only the functions that have been defined but no top-level state. This gives each one a fairly clean starting place. Sometimes, though, you want to continue using the same top-level state from one invocation to the next. + +In this example, we thread the same state through multiple invocations: + +```Rust +extern crate rhai; +use rhai::{Engine, Scope}; + +fn main() { + let mut engine = Engine::new(); + let mut scope: Scope = Vec::new(); + + if let Ok(_) = engine.eval_with_scope(&mut scope, "var x = 4 + 5".to_string()) { } else { assert!(false); } + + if let Ok(result) = engine.eval_with_scope(&mut scope, "x".to_string()).unwrap().downcast::() { + println!("result: {}", result); + } +} +```