| Crates.io | prompt-rust |
| lib.rs | prompt-rust |
| version | 0.1.6 |
| created_at | 2023-11-15 20:22:07.048662+00 |
| updated_at | 2024-12-23 00:06:44.347966+00 |
| description | A library providing a simple input macro for Rust, similar to Python's input(). |
| homepage | |
| repository | https://github.com/bazylhorsey/prompt_rust |
| max_upload_size | |
| id | 1036796 |
| size | 14,673 |
A simple and safe input macro for Rust, inspired by Python's input().
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.
input! macro provides a simple interface for reading user input.Result.Ok(None) on EOF, allowing for graceful handling of end-of-file conditions.use input_macro::input;
fn main() {
// Read a line of text into a String
let name: String = input!("Enter your name: ").unwrap();
println!("Hello, {}!", name);
// Read an integer
let age: i32 = input!("Enter your age: ").unwrap();
println!("You are {} years old.", age);
// Read a floating-point number
let price: f64 = input!().unwrap(); // No prompt
println!("The price is {}.", price);
// Handle potential errors
let num: Result<i32, _> = input!();
match num {
Ok(n) => println!("You entered: {}", n),
Err(e) => eprintln!("Invalid input: {}", e),
}
// Example of handling EOF
loop {
let line: Option<String> = input!().unwrap();
match line {
Some(value) => println!("You entered: {}", value),
None => {
println!("End of input reached.");
break;
}
}
}
}