2

I have the following function that read the user input char by char. By buffering the char in an array, I would like to have a string but fail so far with the error:

expected 'char', found enum termion::event::Key

fn readtest() {
    let terminal = io::stdout().into_raw_mode();
    let mut stdout = terminal.unwrap();

    // Use asynchronous stdin
    let mut stdin = termion::async_stdin().keys();
    let mut s = String::new();

    loop {
        // Read input (if any)
        let input = stdin.next();

        // If a key was pressed
        if let Some(Ok(key)) = input {
            match key {
                // Exit if 'q' is pressed
                termion::event::Key::Char('q') => break,
                // Else print the pressed key
                _ => {
                    s.push(key);
                    stdout.lock().flush().unwrap();
                }
            }
        }
        thread::sleep(time::Duration::from_millis(50));
    }
    s
}

1 Answer 1

2

s is a String and key is a Char(char). You cannot push directly key into s. You first need to use a matching pattern as follows:

 if let termion::event::Key::Char(k) = key {
       s.push(k);
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.