Rustyline is pretty slick

created: 2020-12-26T15:47:01Z
modified: 2020-12-26T15:47:01Z

TS: 2020-04-26T17:38:24.467518

So while working on the CLI CMS side of this blog I started writing some really ugly code to handle command line input. Having recently read something about the Rustyline crate I figured I’d try it out.

So the problem I was trying to solve was the trailing newline in stdin io and customizable input prompts. So I added Rustyline and created this little helper function to handle it.


rustyline = "6.1"


use rustyline::error::ReadlineError;
use rustyline::Editor;
use std::process::exit;

fn input(prompt: &str) -> String {
    let mut editor = Editor::<()>::new();
    let readline = editor.readline(&prompt);
    match readline {
        Ok(line) => {
        Err(ReadlineError::Interrupted) => {
        Err(ReadlineError::Eof) => {
        Err(err) => {
            println!("Error: {:?}", err);

Now I can just do these simple assignments, with the bonus of better error handling and CTRL-C exiting.

let thingy = input("Write a thingy: ");

That lets me replace four lines of not exactly the cleanest code for every basic CLI input with a simple one liner, very nice. There are a lot of other features in Rustyline, but that was enough for me to give it the thumbs up.

  • Creative Commons License
  • Author: Gatewaynode