2022-02-12 05:41:04 +01:00
|
|
|
//! An example showing how to register a simple Rust function.
|
|
|
|
|
2022-01-11 15:12:46 +01:00
|
|
|
use rhai::{Engine, EvalAltResult};
|
2016-03-03 17:08:34 +01:00
|
|
|
|
2022-01-11 15:12:46 +01:00
|
|
|
fn add(x: i64, y: i64) -> i64 {
|
2020-10-07 09:55:45 +02:00
|
|
|
x + y
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:25:12 +02:00
|
|
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
2016-03-03 17:08:34 +01:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2016-03-16 23:07:08 +01:00
|
|
|
engine.register_fn("add", add);
|
2020-03-02 08:19:59 +01:00
|
|
|
|
2022-01-11 15:12:46 +01:00
|
|
|
let result = engine.eval::<i64>("add(40, 2)")?;
|
2020-03-09 09:54:43 +01:00
|
|
|
|
2022-10-27 07:38:21 +02:00
|
|
|
println!("Answer: {result}"); // prints 42
|
2020-03-09 09:54:43 +01:00
|
|
|
|
|
|
|
Ok(())
|
2016-03-03 17:08:34 +01:00
|
|
|
}
|