Very nice, I might use that for some command line tools in the future!
Let me provide an updated example using rustyline
. First, we need a dependency in the Cargo.toml
file:
[dependencies]
rustyline = "9.1.2"
Then we can do:
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rl = rustyline::Editor::<()>::new();
let a: i32 = rl.readline("a = ")?.parse()?;
let b: i32 = rl.readline("b = ")?.parse()?;
let c = a + b;
println!("{a} + {b} = {c}");
Ok(())
}
Looks much cleaner , plus it allows certain shortcuts such as CTRL+A to jump to the beginning of the line, etc.
Downside is a moderately sized tree of dependencies, but I think for interactive command line apps, this might be worth it.