我有以下函数,可以通过char
读取用户输入char
。通过在数组中缓冲 char
,我想要一个字符串,但到目前为止失败并出现错误:
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
}
最佳答案
s 是一个String
,key 是一个Char(char)
。您不能直接将 key 插入 s。您首先需要使用如下匹配模式:
if let termion::event::Key::Char(k) = key {
s.push(k);
}
关于rust - 逐个字符读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64662112/