Refactor some examples.
This commit is contained in:
parent
adaa65f953
commit
710a07d896
@ -1,34 +1,19 @@
|
|||||||
use rhai::{Engine, RegisterFn, Scope};
|
use rhai::{Engine, RegisterFn, Scope};
|
||||||
use std::fmt::Display;
|
|
||||||
use std::io::{stdin, stdout, Write};
|
use std::io::{stdin, stdout, Write};
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
fn showit<T: Display>(x: &mut T) -> () {
|
|
||||||
println!("{}", x)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn quit() {
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
let mut scope = Scope::new();
|
let mut scope = Scope::new();
|
||||||
|
|
||||||
engine.register_fn("print", showit as fn(x: &mut i32) -> ());
|
engine.register_fn("exit", || exit(0));
|
||||||
engine.register_fn("print", showit as fn(x: &mut i64) -> ());
|
|
||||||
engine.register_fn("print", showit as fn(x: &mut u32) -> ());
|
|
||||||
engine.register_fn("print", showit as fn(x: &mut u64) -> ());
|
|
||||||
engine.register_fn("print", showit as fn(x: &mut f32) -> ());
|
|
||||||
engine.register_fn("print", showit as fn(x: &mut f64) -> ());
|
|
||||||
engine.register_fn("print", showit as fn(x: &mut bool) -> ());
|
|
||||||
engine.register_fn("print", showit as fn(x: &mut String) -> ());
|
|
||||||
engine.register_fn("exit", quit);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
print!("> ");
|
print!("> ");
|
||||||
|
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
stdout().flush().expect("couldn't flush stdout");
|
stdout().flush().expect("couldn't flush stdout");
|
||||||
|
|
||||||
if let Err(e) = stdin().read_line(&mut input) {
|
if let Err(e) = stdin().read_line(&mut input) {
|
||||||
println!("input error: {}", e);
|
println!("input error: {}", e);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
use rhai::{Engine, RegisterFn};
|
use rhai::{Engine, RegisterFn};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
fn add(x: i64, y: i64) -> i64 {
|
fn add(x: i64, y: i64) -> i64 {
|
||||||
x + y
|
x + y
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let mut engine = Engine::new();
|
|
||||||
|
|
||||||
engine.register_fn("add", add);
|
engine.register_fn("add", add);
|
||||||
|
|
||||||
if let Ok(result) = engine.eval::<i64>("add(40, 2)") {
|
if let Ok(result) = engine.eval::<i64>("add(40, 2)") {
|
||||||
println!("Answer: {}", result); // prints 42
|
println!("Answer: {}", result); // prints 42
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user