tinyinput
A tiny, dependency-free helper crate for reading and parsing user input from stdin in Rust.
tinyinput is designed for small CLI tools, scripts, and learning projects where you want to read user input without repeating the usual stdin + parse boilerplate.
Why tinyinput?
In Rust, taking user input typically involves:
- Reading a line from
stdin - Trimming whitespace
- Parsing the input into the desired type
While explicit and correct, this pattern quickly becomes repetitive in small programs.
tinyinput provides a minimal and type-safe alternative by leveraging Rust’s compile-time type inference.
The target type is inferred from the assignment context, so no explicit parsing logic is required at the call site.
How to take user input in Rust
Standard Rust approach
use io;
let mut input = Stringnew;
stdin.read_line.unwrap;
let x: i32 = input.trim.parse.unwrap;
Using tinyinput
use read;
let x: i32 = read.unwrap;
This reduces boilerplate while remaining explicit, type-safe, and idiomatic.
Installation
Add tinyinput to your Cargo.toml:
[]
= "0.1"
Example
Run the included example:
use read;
API Overview
read
- Prints the prompt (if non-empty)
- Reads a single line from
stdin - Trims whitespace
- Parses the input into type
T - Returns errors instead of panicking
let value: usize = read.unwrap;
Error Handling
Io— reading from standard input failedParse— input could not be parsed into the requested type
Design Philosophy
- Tiny and focused
- No macros
- No global state
- No hidden panics
- No dependencies
- Explicit error handling
- Leverages Rust’s type inference
This crate is not a replacement for full command-line parsers like clap.
When should you use tinyinput?
- Learning Rust
- Small CLI tools and scripts
- Teaching stdin input handling
- Reducing boilerplate in examples
License
MIT License
Author
Jyotismoy Kalita