Here is a piece of code:
// inside main()
// ask for user input
print!("Enter number: ");
let mut str = String::new();
std::io::stdin().read_line(&mut str);
println!("You have entered {str}");
// main function ends right here
output:
4
Enter number: You have entered 4
Question: Why didn't the print! execute immediately?
stdout is line-buffered. println! ends with a newline, so it automatically flushes the buffer to stdout. print! doesn't automatically print a newline, so you must flush the buffer manually, as @Miiao showed.
I also had to add " use std::io::Write; " at the top. Looks like flush() is part of Write and the Write trait had to be brought into the scope too. Thanks.