Rust stdin
Nonblocking
OS | Status |
---|---|
Ubuntu-latest | |
macOS-latest | |
Windows-latest |
Dependency-less non-blocking stdin
reader using background threads. Supports streaming and immediate fallback defaults.
Supports binary data, streaming, and immediate fallback defaults.
Install
Usage
Get stdin
or Default
use get_stdin_or_default;
// If running in interactive mode (stdin is a terminal),
// `get_stdin_or_default` returns the default value immediately.
let input = get_stdin_or_default;
// Input is always `Vec<u8>`, ensuring binary safety.
assert_eq!;
Read stdin
as Stream
use spawn_stdin_stream;
use TryRecvError;
use Duration;
// If running in interactive mode (stdin is a terminal),
// `spawn_stdin_stream` returns an empty receiver, meaning no input will be received.
let stdin_stream = spawn_stdin_stream;
loop
Use with Tokio
Refer to the included Tokio Example App.
Related threads
- https://stackoverflow.com/questions/30012995/how-can-i-read-non-blocking-from-stdin
- https://www.reddit.com/r/rust/comments/fc71ju/how_to_read_from_stdin_without_blocking/?rdt=55515
License
MIT License (c) 2025 Jeremy Harris.