Closed
Description
As stdout is flushed only when a newline character is printed, the following program does not work properly:
print!("Input a text: ");
let mut text = String::new();
stdin().read_line(&mut text).unwrap();
println!("Text: {}", text);
Therefore, it is required to flush explicitly like stdout().flush().unwrap()
. #23818 suggested always calling flush if anything is printed, but it is probably not acceptable due to the performance concerns.
However, at least C and Python seem to have an implicit flushing behavior when scanf()
/input()
is called, so the following works as expected:
print('Input a text: ', end='')
text = input()
print('Text: {}'.format(text))
printf("Input a text: ");
char text[1024];
fgets(text, sizeof(text), stdin);
printf("Text: %s", text);
It would also be a good idea to adopt a similar strategy in Rust.
Metadata
Metadata
Assignees
Labels
No labels