Provoking an error in stdin().read_line()

Viewed 43

Consider the following short program:

use std::io::{stdin, stdout, Write};

fn main() {
    loop {
        let mut command = prompt();
        println!("--> {}", command);
    }
}

fn prompt() -> String {
    let mut buffer = String::new();
    print!("++> ");
    let _ = stdout().flush();
    stdin().read_line(&mut buffer).expect("Error retrieving input.");
    buffer.clone()
}

The result is a pretty clear looping input program - it presents a prompt, the user can type whatever and it's echoed back. What I want to know is how would I provoke the expect() clause to be fired? I understand that read_line() returns a Result and that expect() unpacks it if it is Ok, but I'm not sure how to practically (and manually or in a test case) force an error here to get the panic() result from expect(). I've tried a lot of different character inputs and keyboard combos but haven't found one that doesn't just quit the program or that it handles cleanly.

0 Answers
Related