Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdout/stderr should be flushed when reading from stdin #25555

Closed
barosl opened this issue May 17, 2015 · 3 comments
Closed

stdout/stderr should be flushed when reading from stdin #25555

barosl opened this issue May 17, 2015 · 3 comments

Comments

@barosl
Copy link
Contributor

barosl commented May 17, 2015

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.

@emberian
Copy link
Member

This seems like a decent "Do What I Mean" fix.

@nagisa
Copy link
Member

nagisa commented May 18, 2015

We used to do this, but do not anymore (#23087) because it used to be a very easy way to cause a deadlock. Since stdio now uses reentrant mutexes (#24029), I believe this functionality can could be introduced again.

nagisa added a commit to nagisa/rust that referenced this issue May 18, 2015
This is possible now since we have reentrant mutexes. Fixes rust-lang#25555.
nagisa added a commit to nagisa/rust that referenced this issue May 18, 2015
This is possible now since we have reentrant mutexes. Fixes rust-lang#25555.
nagisa added a commit to nagisa/rust that referenced this issue May 18, 2015
This is possible now since we have reentrant mutexes. Fixes rust-lang#25555.
@alexcrichton
Copy link
Member

The associated PR for this issue was closed, so I'm going to close this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants