A question about std::io::stdin

I write a piece of code like this.

use std::io::stdin;
fn main(){
    let mut buf = String::new();
    print!("Please enter your name:");
    stdin().read_line(&mut buf).expect("Failed to read");
    println!();
    print!("{}",buf);
}

What do I expected is like

Please enter your name:John Doe
John Doe

But the actual is like

John Doe
Please enter your name:John Doe

Is there any way to realize the effect that I expected in Rust?(crates are ok,too)

Take a look at the answers here. It's not the same starting point, but the answrs are still relevant. You'll need to flush stdout.

https://stackoverflow.com/questions/40392906/no-method-named-flush-found-for-type-stdiostdout-in-the-current-scope

 io::stdout().flush().unwrap();

works

Thank you

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.