| Crates.io | stdin-nonblocking |
| lib.rs | stdin-nonblocking |
| version | 0.4.1 |
| created_at | 2025-03-03 21:39:31.745618+00 |
| updated_at | 2025-03-13 04:54:58.335883+00 |
| description | Dependency-less non-blocking stdin reader using background threads. Supports streaming and immediate fallback defaults. |
| homepage | |
| repository | https://github.com/jzombie/rust-stdin-nonblocking |
| max_upload_size | |
| id | 1576367 |
| size | 15,951 |
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.
cargo add stdin-nonblocking
stdin or Defaultuse stdin_nonblocking::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(Some(b"fallback_value"));
// Input is always `Vec<u8>`, ensuring binary safety.
assert_eq!(input, Some(b"fallback_value".to_vec()));
stdin as Streamuse stdin_nonblocking::spawn_stdin_stream;
use std::sync::mpsc::TryRecvError;
use std::time::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 {
match stdin_stream.try_recv() {
Ok(bytes) => println!("Received: {:?}", bytes), // Always raw bytes
Err(TryRecvError::Empty) => {
// No input yet; continue execution
}
Err(TryRecvError::Disconnected) => {
println!("Input stream closed. Exiting...");
break;
}
}
std::thread::sleep(Duration::from_millis(500));
}
Refer to the included Tokio Example App.
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
MIT License (c) 2025 Jeremy Harris.