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

docs: Clarify std::io::Stdin::read_to_string behavior #86613

Closed
ayyEve opened this issue Jun 25, 2021 · 7 comments
Closed

docs: Clarify std::io::Stdin::read_to_string behavior #86613

ayyEve opened this issue Jun 25, 2021 · 7 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-io Area: std::io, std::fs, std::net and std::path C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@ayyEve
Copy link

ayyEve commented Jun 25, 2021

Hi all,
I'm getting a weird stdin blocking issue. when reading from stdin, the console appears to freeze, nothing except what i type is written to the console. however, on windows, when the app is terminated the console buffer is "flushed", and all missing output is written, this doesnt seem to happen on linux (again, through ssh).
I originally encountered this issue on windows, but i was able to replicate it on linux with an (slightly) older toolchain (through ssh)
I will test beta, nightly, and a much older toolchain soon.

I tried this code:

use std::io::Read;
fn main() -> std::io::Result<()> {
    println!("type your input");
    
    let mut buffer = String::new();
    std::io::stdin().read_to_string(&mut buffer)?;

    println!("got input {}", buffer);
    Ok(())
}

I expected to see this happen: "type your input" logged, the message i typed, and after hitting enter, "got input {X}" being logged, and the program closing

Instead, this happened: *"type out input" is logged, my message appears as im writing, but hitting enter does not input the message, almost like it inserts a newline instead. after this, the program must be terminated with ctrl+c *
linux:
image
windows:
image

Meta

rustc --version --verbose:

(linux)
rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-unknown-linux-gnu
release: 1.52.1
LLVM version: 12.0.0

(windows)
rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-pc-windows-msvc
release: 1.53.0
LLVM version: 12.0.1
Backtrace

N/A, the code does not crash

lmk if i forgot anything and i'll update it asap

@ayyEve ayyEve added the C-bug Category: This is a bug. label Jun 25, 2021
@AngelOnFira
Copy link
Member

I tested this in Docker on Linux with Rust 1.33 from Feb 2019, and got the same result.

root@359301ed4de1:/app# rustup show
Default host: x86_64-unknown-linux-gnu
rustup home:  /usr/local/rustup

installed toolchains
--------------------

1.33-x86_64-unknown-linux-gnu
1.53.0-x86_64-unknown-linux-gnu (default)

active toolchain
----------------

1.33-x86_64-unknown-linux-gnu (overridden by '/app/rust-toolchain')
rustc 1.33.0 (2aa4c46cf 2019-02-28)

@fee1-dead
Copy link
Member

This might be expected behavior on linux that is platform-specific, but I am not sure though. read_to_string would read from standard input until EOF is reached (you can input EOF by pressing Ctrl+D on the start of a new line). I don't know about windows though.

@jonas-schievink
Copy link
Contributor

This works correctly on Linux/Unix. stdin only closes when pressing Ctrl+D and read_to_string will block until the stream is closed. Not sure about the Windows behavior though, that looks odd.

@ayyEve
Copy link
Author

ayyEve commented Jun 25, 2021

Hi all, so it appears this is working as expected (at least on linux). however, I personally think the docs should explain this a bit better.
The example for stdin() is the read_to_string, however i am used to stdin being used for user input, not piped input. This example only works for piped input, however it doesnt specify this, so a noob (like me) can easily have issues.
It would be nice if there was also a read_line example for stdin() which explains the differences between it and read_to_string.

@inquisitivecrystal
Copy link
Contributor

@rustbot label A-io T-libs

@rustbot rustbot added A-io Area: std::io, std::fs, std::net and std::path T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 28, 2021
@JohnTitor JohnTitor added A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Jul 1, 2021
@JohnTitor JohnTitor changed the title stdin hangs docs: Clarify std::io::Stdin::read_to_string behavior Jul 1, 2021
@ben0x539
Copy link
Contributor

Is this fixed by #87820?

@ayyEve
Copy link
Author

ayyEve commented Oct 27, 2021

yep, that resolves this issue

@ayyEve ayyEve closed this as completed Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-io Area: std::io, std::fs, std::net and std::path C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants