Ctrl+Z not available as EOF on Windows PowerShell

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

Give up on windows is my advice.

Could probably try update to 1.72

2 Likes

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.