input_macro
A simple and safe input macro for Rust, inspired by Python's input()
.
Motivation
Rust's standard library currently lacks a simple way to read user input and parse it into different types, similar to Python's input()
. This can make simple programs and examples more complex than necessary, especially for beginners. This library aims to fill that gap by providing a user-friendly macro.
Features
- Easy to use: The
input!
macro provides a simple interface for reading user input. - Type inference: The macro automatically infers the desired type based on the variable it's assigned to.
- Error handling: It handles both I/O errors and parsing errors gracefully, returning a
Result
. - Optional prompt: You can provide an optional prompt string that will be printed before reading input.
- Flushing: The prompt is automatically flushed to ensure it's visible to the user before input is read.
- Safety: The macro is designed to be safe and prevent common errors like buffer overflows.
- Performance: It's built on top of Rust's efficient I/O and string handling.
- EOF Handling: Returns
Ok(None)
on EOF, allowing for graceful handling of end-of-file conditions.
Usage Examples
use input;