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() {
let mut engine = Engine::new();
engine.register_type::<TestStruct>();
engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new);
engine
.register_type::<TestStruct>()
.register_fn("update", TestStruct::update)
.register_fn("new_ts", TestStruct::new);
println!(
"{:?}",

View File

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

View File

@ -1,12 +1,12 @@
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
fn add(x: INT, y: INT) -> INT {
x + y
}
fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
fn add(x: INT, y: INT) -> INT {
x + y
}
engine.register_fn("add", add);
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();
// Register string functions
engine.register_fn("trim", trim_string);
engine.register_fn("len", count_string_bytes);
engine.register_fn("index_of", find_substring);
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
engine.register_fn("display", |label: &str, x: INT| {
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
});