diff --git a/examples/arrays_and_structs.rs b/examples/arrays_and_structs.rs index 094a7d08..aac28801 100644 --- a/examples/arrays_and_structs.rs +++ b/examples/arrays_and_structs.rs @@ -9,7 +9,6 @@ impl TestStruct { pub fn update(&mut self) { self.x += 1000; } - pub fn new() -> Self { Self { x: 1 } } @@ -25,17 +24,30 @@ fn main() -> Result<(), Box> { .register_fn("new_ts", TestStruct::new) .register_fn("update", TestStruct::update); - println!( - "{:?}", - engine.eval::("let x = new_ts(); x.update(); x")? - ); - println!( - "{:?}", - engine.eval::("let x = [new_ts()]; x[0].update(); x[0]")? - ); + let result = engine.eval::( + r" + let x = new_ts(); + x.update(); + x + ", + )?; + + println!("{:?}", result); + + let result = engine.eval::( + r" + let x = [ new_ts() ]; + x[0].update(); + x[0] + ", + )?; + + println!("{:?}", result); Ok(()) } #[cfg(any(feature = "no_index", feature = "no_object"))] -fn main() {} +fn main() { + panic!("This example does not run under 'no_index' or 'no_object'.") +} diff --git a/examples/custom_types_and_methods.rs b/examples/custom_types_and_methods.rs index c48a7fde..5d494fd3 100644 --- a/examples/custom_types_and_methods.rs +++ b/examples/custom_types_and_methods.rs @@ -24,7 +24,13 @@ fn main() -> Result<(), Box> { .register_fn("new_ts", TestStruct::new) .register_fn("update", TestStruct::update); - let result = engine.eval::("let x = new_ts(); x.update(); x")?; + let result = engine.eval::( + r" + let x = new_ts(); + x.update(); + x + ", + )?; println!("result: {}", result.x); // prints 1001 @@ -32,4 +38,6 @@ fn main() -> Result<(), Box> { } #[cfg(feature = "no_object")] -fn main() {} +fn main() { + panic!("This example does not run under 'no_object'."); +} diff --git a/examples/hello.rs b/examples/hello.rs index 00913e2c..4133cd3f 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -3,6 +3,8 @@ use rhai::{Engine, EvalAltResult, INT}; fn main() -> Result<(), Box> { let engine = Engine::new(); + engine.consume(r#"print("hello, world!")"#)?; + let result = engine.eval::("40 + 2")?; println!("Answer: {}", result); // prints 42 diff --git a/examples/reuse_scope.rs b/examples/reuse_scope.rs index a762ac8a..e7446e55 100644 --- a/examples/reuse_scope.rs +++ b/examples/reuse_scope.rs @@ -6,9 +6,15 @@ fn main() -> Result<(), Box> { engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?; - let result = engine.eval_with_scope::(&mut scope, "x")?; + println!("x = {}", scope.get_value::("x").unwrap()); - println!("result: {}", result); + for _ in 0..10 { + let result = engine.eval_with_scope::(&mut scope, "x += 1; x")?; + + println!("result: {}", result); + } + + println!("x = {}", scope.get_value::("x").unwrap()); Ok(()) } diff --git a/examples/serde.rs b/examples/serde.rs index eca31453..bf40499d 100644 --- a/examples/serde.rs +++ b/examples/serde.rs @@ -1,6 +1,6 @@ #[cfg(not(feature = "serde"))] fn main() { - println!(r#"This example requires the "serde" feature which is not enabled by default."#); + println!("This example requires the 'serde' feature to run."); println!("Try: cargo run --features serde --example serde"); } diff --git a/examples/strings.rs b/examples/strings.rs index dc5f76b3..2ea97511 100644 --- a/examples/strings.rs +++ b/examples/strings.rs @@ -33,12 +33,12 @@ fn main() -> Result<(), Box> { .register_fn("trim", trim_string) .register_fn("len", count_string_bytes) .register_fn("index_of", find_substring) - .register_fn("display", |label: &str, x: INT| { - // Register string functions using closures - println!("{}: {}", label, x) + // Register string functions using closures + .register_fn("display", |label: &str, value: INT| { + println!("{}: {}", label, value) }) - .register_fn("display", |label: ImmutableString, x: &str| { - println!(r#"{}: "{}""#, label, x) // Quote the input string + .register_fn("display", |label: ImmutableString, value: &str| { + println!(r#"{}: "{}""#, label, value) // Quote the input string }); let mut scope = Scope::new(); diff --git a/examples/threading.rs b/examples/threading.rs index 527b9214..f28da439 100644 --- a/examples/threading.rs +++ b/examples/threading.rs @@ -1,5 +1,8 @@ use rhai::{Engine, RegisterFn, INT}; +#[cfg(feature = "sync")] +use std::sync::Mutex; + fn main() { // Channel: Script -> Master let (tx_script, rx_master) = std::sync::mpsc::channel(); @@ -7,10 +10,7 @@ fn main() { let (tx_master, rx_script) = std::sync::mpsc::channel(); #[cfg(feature = "sync")] - let (tx_script, rx_script) = ( - std::sync::Mutex::new(tx_script), - std::sync::Mutex::new(rx_script), - ); + let (tx_script, rx_script) = (Mutex::new(tx_script), Mutex::new(rx_script)); // Spawn thread with Engine std::thread::spawn(move || { @@ -22,7 +22,7 @@ fn main() { #[cfg(not(feature = "sync"))] engine - .register_fn("get", move || rx_script.recv().unwrap()) + .register_fn("get", move || rx_script.recv().unwrap_or_default()) .register_fn("put", move |v: INT| tx_script.send(v).unwrap()); #[cfg(feature = "sync")]