stdin-nonblocking 0.2.0

Dependency-less non-blocking stdin reader using background threads. Supports streaming and immediate fallback defaults.
Documentation

Rust stdin Nonblocking

made-with-rust crates.io Documentation MIT licensed

OS Status
Ubuntu-latest Ubuntu Tests
macOS-latest macOS Tests
Windows-latest Windows Tests

Dependency-less non-blocking stdin reader using background threads. Supports streaming and immediate fallback defaults.

Install

cargo add stdin-nonblocking

Usage

Get stdin or Default

use stdin_nonblocking::get_stdin_or_default;

let input = get_stdin_or_default(Some("fallback_value"));

assert_eq!(input, Some("fallback_value".to_string()));

Read stdin as Stream

use stdin_nonblocking::spawn_stdin_stream;
use std::sync::mpsc::TryRecvError;
use std::time::Duration;

let stdin_stream = spawn_stdin_stream();

loop {
    match stdin_stream.try_recv() {
        Ok(line) => println!("Received: {}", line),
        Err(TryRecvError::Empty) => {
            // No input yet; continue execution
        }
        Err(TryRecvError::Disconnected) => {
            println!("Input stream closed. Exiting...");
            break;
        }
    }
    std::thread::sleep(Duration::from_millis(500));
}

Use with Tokio

Refer to the included Tokio Example App.

Related threads

License

MIT License (c) 2025 Jeremy Harris.