mlua/examples/repl.rs
Jonas Schievink 8bd0c2c812 Rename LuaString to String
This required a lot of little adjustments where we used std's `String`
before. In downstream code, this shouldn't be necessary, as you can just
do `use rlua::String as LuaString` to disambiguate.
2017-07-23 18:36:50 +02:00

48 lines
1.3 KiB
Rust

//! This example shows a simple read-evaluate-print-loop (REPL).
extern crate rlua;
use rlua::{Lua, MultiValue, Error};
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();
loop {
stdin.read_line(&mut line).unwrap();
match lua.eval::<MultiValue>(&line, None) {
Ok(values) => {
println!(
"{}",
values
.iter()
.map(|value| format!("{:?}", value))
.collect::<Vec<_>>()
.join("\t")
);
break;
}
Err(Error::IncompleteStatement(_)) => {
// continue reading input and append it to `line`
write!(stdout, ">> ").unwrap();
stdout.flush().unwrap();
}
Err(e) => {
writeln!(stderr(), "error: {}", e).unwrap();
break;
}
}
}
}
}