A micro-library for keyboard input in Rust. No extra code — just input and get values!
ask_input is a tiny wrapper over Rust's standard I/O. One function. All types. Rust figures out the type automatically.
- 🎯 One function for everything
- 🧠 Smart type detection
- 🛡️ Automatic stdout flushing to prevent prompt-freezing
- ⚡ Zero dependencies
Add to your Cargo.toml:
[dependencies]
ask_input = "0.3.0"use ask_input::input;fnmain(){let age:i32 = input().expect("Failed to read age");let price:f64 = input().expect("Failed to read price");let name:String = input().expect("Failed to read name");println!("Age: {}, Price: {}, Name: {}", age, price, name);}use ask_input::input;fnmain(){println!("Enter your age:");let age:i32 = matchinput(){Ok(num) => num,Err(_) => {println!("Invalid input! Using default age 18.");18}};println!("Your age: {}", age);}use ask_input::input;fnmain(){// If input fails, use 0.0 as defaultlet price:f64 = input().unwrap_or(0.0);println!("Price: {}", price);}input::<T>()— Input any type (i32, f64, String, etc.)
| Type | Example | Notes |
|---|---|---|
i32 | 42 | Leading/trailing whitespace trimmed |
f64 | 3.14 | Leading/trailing whitespace trimmed |
String | Hello | Whitespace preserved (only newline removed) |
bool | true | Case-sensitive, whitespace trimmed |
i64, u32... | Any numeric | Whitespace trimmed |
int_input()→input::<i32>()float_input()→input::<f64>()str_input()→input::<String>()- Now returns
Resultinstead of panicking
- Added automatic
io::stdout().flush()before reading input. This guarantees thatprint!prompts appear on screen instantly, fixing terminal output freezing. - Updated all inline documentation to English.
- FelineFantasy
- License: MIT