From e7f2dc84f1743700e2224b8a965fdebe3c06474a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 7 Oct 2020 15:55:45 +0800 Subject: [PATCH] Revise examples. --- examples/arrays_and_structs.rs | 8 ++++---- examples/custom_types_and_methods.rs | 8 ++++---- examples/simple_fn.rs | 8 ++++---- examples/strings.rs | 22 +++++++++++----------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/examples/arrays_and_structs.rs b/examples/arrays_and_structs.rs index 5e604120..6aa9fafa 100644 --- a/examples/arrays_and_structs.rs +++ b/examples/arrays_and_structs.rs @@ -20,10 +20,10 @@ impl TestStruct { fn main() { let mut engine = Engine::new(); - engine.register_type::(); - - engine.register_fn("update", TestStruct::update); - engine.register_fn("new_ts", TestStruct::new); + engine + .register_type::() + .register_fn("update", TestStruct::update) + .register_fn("new_ts", TestStruct::new); println!( "{:?}", diff --git a/examples/custom_types_and_methods.rs b/examples/custom_types_and_methods.rs index d152dc9b..4a8e260a 100644 --- a/examples/custom_types_and_methods.rs +++ b/examples/custom_types_and_methods.rs @@ -19,10 +19,10 @@ impl TestStruct { fn main() -> Result<(), Box> { let mut engine = Engine::new(); - engine.register_type::(); - - engine.register_fn("update", TestStruct::update); - engine.register_fn("new_ts", TestStruct::new); + engine + .register_type::() + .register_fn("update", TestStruct::update) + .register_fn("new_ts", TestStruct::new); let result = engine.eval::("let x = new_ts(); x.update(); x")?; diff --git a/examples/simple_fn.rs b/examples/simple_fn.rs index 21f532ae..c0c59bad 100644 --- a/examples/simple_fn.rs +++ b/examples/simple_fn.rs @@ -1,12 +1,12 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT}; +fn add(x: INT, y: INT) -> INT { + x + y +} + fn main() -> Result<(), Box> { let mut engine = Engine::new(); - fn add(x: INT, y: INT) -> INT { - x + y - } - engine.register_fn("add", add); let result = engine.eval::("add(40, 2)")?; diff --git a/examples/strings.rs b/examples/strings.rs index e9d53016..f24e9545 100644 --- a/examples/strings.rs +++ b/examples/strings.rs @@ -29,17 +29,17 @@ fn main() -> Result<(), Box> { let mut engine = Engine::new_raw(); // Register string functions - engine.register_fn("trim", trim_string); - engine.register_fn("len", count_string_bytes); - engine.register_fn("index_of", find_substring); - - // Register string functions using closures - engine.register_fn("display", |label: &str, x: INT| { - println!("{}: {}", label, x) - }); - engine.register_fn("display", |label: ImmutableString, x: &str| { - println!(r#"{}: "{}""#, label, x) // Quote the input string - }); + engine + .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_fn("display", |label: ImmutableString, x: &str| { + println!(r#"{}: "{}""#, label, x) // Quote the input string + }); let mut scope = Scope::new(); let mut input = String::new();