如何从输出的同一行读取键盘上的字符串? [英] How to read a string from keyboard on the same line with output?
本文介绍了如何从输出的同一行读取键盘上的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在读这样的字符串:
I'm reading a string like this:
print!("Input string: ");
let string: String = String::new();
std::io::stdin().read_line(&mut string);
当我启动程序时,我看到:
When I launch the program I see:
(write a string here)
Input string:
但我需要:
Input string: (write a string here)
如何实现?
推荐答案
添加调用 stdout()。flush()
在调用 read_line
之前强制缓冲区输出:
Add a call to stdout().flush()
to force the buffer to output before read_line
is called:
fn main() {
print!("Input string: ");
std::io::stdout().flush();
let mut string: String = String::new();
std::io::stdin().read_line(&mut string);
}
这篇关于如何从输出的同一行读取键盘上的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文