Use run and i64 instead of eval and INT for examples.

This commit is contained in:
Stephen Chung
2022-01-11 22:12:46 +08:00
parent 6048c5804b
commit f0e9d4a557
7 changed files with 25 additions and 25 deletions

View File

@@ -1,6 +1,6 @@
///! This example registers a variety of functions that operate on strings.
///! Remember to use `ImmutableString` or `&str` instead of `String` as parameters.
use rhai::{Engine, EvalAltResult, ImmutableString, Scope, INT};
use rhai::{Engine, EvalAltResult, ImmutableString, Scope};
use std::io::{stdin, stdout, Write};
/// Trim whitespace from a string. The original string argument is changed.
@@ -15,26 +15,26 @@ fn trim_string(s: &mut ImmutableString) {
/// This version simply counts the number of _bytes_ in the UTF-8 representation.
///
/// This version uses `&str`.
fn count_string_bytes(s: &str) -> INT {
s.len() as INT
fn count_string_bytes(s: &str) -> i64 {
s.len() as i64
}
/// This version uses `ImmutableString` and `&str`.
fn find_substring(s: ImmutableString, sub: &str) -> INT {
s.find(sub).map(|x| x as INT).unwrap_or(-1)
fn find_substring(s: ImmutableString, sub: &str) -> i64 {
s.find(sub).map(|x| x as i64).unwrap_or(-1)
}
fn main() -> Result<(), Box<EvalAltResult>> {
// Create a `raw` Engine with no built-in string functions.
let mut engine = Engine::new_raw();
// Register string functions
engine
// Register string functions
.register_fn("trim", trim_string)
.register_fn("len", count_string_bytes)
.register_fn("index_of", find_substring)
// Register string functions using closures
.register_fn("display", |label: &str, value: INT| {
.register_fn("display", |label: &str, value: i64| {
println!("{}: {}", label, value)
})
.register_fn("display", |label: ImmutableString, value: &str| {