Maybe it's a bug, but I'm not sure.
When running the following program, read_to_string
will not return successfully if I press Ctrl+Z
.
use std::io::Read;
fn main() {
let stdin = std::io::stdin();
let mut h_stdin = stdin.lock();
let mut buf = String::new();
h_stdin.read_to_string(&mut buf).unwrap();
println!("{buf}");
}
All I got would be a panic, which says
Windows stdin in console mode does not support non-UTF-16 input; encountered unpaired surrogate
The rustc version is 1.71.1 (eb26296b5 2023-08-03).
I'm using Windows Terminal and Windows PowerShell 7.3.6.
Just to corroborate: the same problem happens with CMD in conhost, PowerShell in conhost, and nushell in Windows Terminal. Specifically:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: InvalidData, message: "Windows stdin in console mode does not support non-UTF-16 input; encountered unpaired surrogate" }', src\main.rs:7:38
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 Like
Full message is:
PS D:\> ./main.exe
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: InvalidData, message: "Windows stdin in console mode does not support non-UTF-16 input; encountered unpaired surrogate" }', main.rs:7:38
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
jonh
September 2, 2023, 9:56am
4
Give up on windows is my advice.
Could probably try update to 1.72
opened 12:22AM - 31 May 23 UTC
closed 02:36AM - 02 Jun 23 UTC
O-windows
C-bug
T-libs
<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the… bug,
along with any information you feel relevant to replicating the bug.
-->
I tried this code:
```rust
use std::io::stdin;
fn main() {
for line in stdin().lines() {
let line = line.unwrap();
println!("{}", line);
}
}
```
In windows terminal running Git Bash (also occured in Nushell, though I'm not sure if this is terminal or shell-land), I pressed Ctrl-Z + Enter to send EOF.
I expected to see this happen: The application would gracefully exit the loop
Instead, this happened: I saw this error message:
```
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: InvalidData, message: "Windows stdin in console mode does not support non-UTF-16 input; encountered unpaired surrogate" }', src\main.rs:5:25
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\eof.exe` (exit code: 101)
```
### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->
`rustc --version --verbose`:
```
rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-pc-windows-msvc
release: 1.69.0
LLVM version: 15.0.7
```
<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Backtrace</summary>
<p>
```
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: InvalidData, message: "Windows stdin in console mode does not support non-UTF-16 input; encountered unpaired surrogate" }', src\main.rs:5:25
stack backtrace:
0: std::panicking::begin_panic_handler
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\std\src\panicking.rs:579
1: core::panicking::panic_fmt
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\core\src\panicking.rs:64
2: core::result::unwrap_failed
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\core\src\result.rs:1750
3: enum2$<core::result::Result<alloc::string::String,std::io::error::Error> >::unwrap<alloc::string::String,std::io::error::Error>
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc\library\core\src\result.rs:1090
4: eof::main
at .\src\main.rs:5
5: core::ops::function::FnOnce::call_once<enum2$<core::result::Result<tuple$<>,std::io::error::Error> > (*)(),tuple$<> >
at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc\library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\eof.exe` (exit code: 101)
```
</p>
</details>
2 Likes
system
Closed
December 1, 2023, 9:57am
5
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.