Revise examples.

This commit is contained in:
Stephen Chung 2020-10-07 15:55:45 +08:00
parent bc8aa470cb
commit e7f2dc84f1
4 changed files with 23 additions and 23 deletions

View File

@ -20,10 +20,10 @@ impl TestStruct {
fn main() { fn main() {
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.register_type::<TestStruct>(); engine
.register_type::<TestStruct>()
engine.register_fn("update", TestStruct::update); .register_fn("update", TestStruct::update)
engine.register_fn("new_ts", TestStruct::new); .register_fn("new_ts", TestStruct::new);
println!( println!(
"{:?}", "{:?}",

View File

@ -19,10 +19,10 @@ impl TestStruct {
fn main() -> Result<(), Box<EvalAltResult>> { fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.register_type::<TestStruct>(); engine
.register_type::<TestStruct>()
engine.register_fn("update", TestStruct::update); .register_fn("update", TestStruct::update)
engine.register_fn("new_ts", TestStruct::new); .register_fn("new_ts", TestStruct::new);
let result = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?; let result = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?;

View File

@ -1,12 +1,12 @@
use rhai::{Engine, EvalAltResult, RegisterFn, INT}; use rhai::{Engine, EvalAltResult, RegisterFn, INT};
fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
fn add(x: INT, y: INT) -> INT { fn add(x: INT, y: INT) -> INT {
x + y x + y
} }
fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_fn("add", add); engine.register_fn("add", add);
let result = engine.eval::<INT>("add(40, 2)")?; let result = engine.eval::<INT>("add(40, 2)")?;

View File

@ -29,15 +29,15 @@ fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new_raw(); let mut engine = Engine::new_raw();
// Register string functions // Register string functions
engine.register_fn("trim", trim_string); engine
engine.register_fn("len", count_string_bytes); .register_fn("trim", trim_string)
engine.register_fn("index_of", find_substring); .register_fn("len", count_string_bytes)
.register_fn("index_of", find_substring)
.register_fn("display", |label: &str, x: INT| {
// Register string functions using closures // Register string functions using closures
engine.register_fn("display", |label: &str, x: INT| {
println!("{}: {}", label, x) println!("{}: {}", label, x)
}); })
engine.register_fn("display", |label: ImmutableString, x: &str| { .register_fn("display", |label: ImmutableString, x: &str| {
println!(r#"{}: "{}""#, label, x) // Quote the input string println!(r#"{}: "{}""#, label, x) // Quote the input string
}); });