tinyinput 0.1.1

A tiny, generic helper for reading and parsing user input from stdin using Rust’s type inference.
Documentation
  • Coverage
  • 83.33%
    5 out of 6 items documented2 out of 3 items with examples
  • Size
  • Source code size: 9.32 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.34 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 16s Average build duration of successful builds.
  • all releases: 17s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • JyotismoyKalita/tinyinput
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • JyotismoyKalita

tinyinput

License: MIT Rust Crates.io Docs.rs

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:

  1. Reading a line from stdin
  2. Trimming whitespace
  3. 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 std::io;

let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let x: i32 = input.trim().parse().unwrap();

Using tinyinput

use tinyinput::read;

let x: i32 = read("Enter number: ").unwrap();

This reduces boilerplate while remaining explicit, type-safe, and idiomatic.


Installation

Add tinyinput to your Cargo.toml:

[dependencies]
tinyinput = "0.1"

Example

Run the included example:

cargo run --example demo
use tinyinput::read;

fn main() {
    let count: i32 = read("Enter count: ").unwrap();
    let ratio: f64 = read("Enter ratio: ").unwrap_or_default();
    let name: String = read("Enter name: ").unwrap();

    println!("{count}, {ratio}, {name}");
}

API Overview

read

pub fn read<T>(prompt: &str) -> Result<T, ReadError>
where
    T: FromStr,
  • 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("Enter value: ").unwrap();

Error Handling

pub enum ReadError {
    Io(std::io::Error),
    Parse,
}
  • Io — reading from standard input failed
  • Parse — 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