Crate tiny_input

Source
Expand description

Tiny input macros.

This crate provides three macros for receiving user input: tiny_input!, input! and raw_input!.

raw_input! is used for when you just need the string (while handling I/O errors):

use tiny_input::raw_input;

let name = raw_input!("What is your name? ").unwrap();

println!("Hello, {name}!");

tiny_input! is useful for when panicking on I/O errors is fine, and you only need to parse the input:

use tiny_input::tiny_input;

let value: u64 = tiny_input!("the square of ").unwrap();

println!("is {}", value * value);

input! is when you need to handle both I/O and parsing errors:

use tiny_input::{input, Error};

match input!(as u64, "the square of ") {
    Ok(value) => println!("is {}", value * value),
    Err(error) => match error {
        Error::Fetch(fetch_error) => eprintln!("failed to fetch: {fetch_error}"),
        Error::Parse(parse_error) => eprintln!("failed to parse: {parse_error}"),
    },
}

As one might have noticed, there are two kinds of tiny_input! and input!, one that attempts to infer the type, and one where you can provide the type explicitly.

Macros§

input
Similar to tiny_input!, except I/O and parse errors are wrapped into Error<E>.
raw_input
Fetches raw inputs, returning the resulting String and propagating I/O errors.
tiny_input
Invokes raw_input!, panicking on I/O errors before parsing the string.

Enums§

Error
Represents errors that can occur when processing inputs.

Constants§

FETCH_ERROR
The message used for expecting values.

Type Aliases§

Result
The specialized result type to be used in this library.