Add a simple repl example

This commit is contained in:
Jonas Schievink 2017-06-17 01:19:21 +02:00
parent 484479477b
commit 4f05516783
1 changed files with 30 additions and 0 deletions

30
examples/repl.rs Normal file
View File

@ -0,0 +1,30 @@
//! This example shows a simple read-evaluate-print-loop (REPL).
extern crate rlua;
use rlua::*;
use std::io::prelude::*;
use std::io::{stdin, stdout, stderr, BufReader};
fn main() {
let lua = Lua::new();
let mut stdout = stdout();
let mut stdin = BufReader::new(stdin());
loop {
write!(stdout, "> ").unwrap();
stdout.flush().unwrap();
let mut line = String::new();
stdin.read_line(&mut line).unwrap();
match lua.eval::<LuaMultiValue>(&line) {
Ok(values) => {
println!("{}", values.iter().map(|value| format!("{:?}", value)).collect::<Vec<_>>().join("\t"));
}
Err(e) => {
writeln!(stderr(), "error: {}", e).unwrap();
}
}
}
}