Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a string from keyboard on the same line with output? [duplicate]

Tags:

string

io

rust

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:

But I need:

Input string: (write a string here)

How to implement this?

like image 959
goopycs Avatar asked Jun 26 '15 14:06

goopycs


1 Answers

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);
}
like image 189
bedwyr Avatar answered Oct 06 '22 00:10

bedwyr