Revise examples.
This commit is contained in:
parent
bc8aa470cb
commit
e7f2dc84f1
@ -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!(
|
||||
"{:?}",
|
||||
|
@ -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")?;
|
||||
|
||||
|
@ -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)")?;
|
||||
|
@ -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
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user