| Crates.io | input_py |
| lib.rs | input_py |
| version | 1.0.0 |
| created_at | 2023-09-20 02:28:52.077322+00 |
| updated_at | 2025-08-17 10:41:01.958364+00 |
| description | you can use input like python3 |
| homepage | |
| repository | |
| max_upload_size | |
| id | 977743 |
| size | 19,824 |
input_py is a simple Rust library that provides Python-like input functionality for reading user input from the terminal.
input() function similar to PythonAdd this to your Cargo.toml:
[dependencies]
input_py = "1.0.0"
use input_py::input;
fn main() {
match input("Enter your name") {
Ok(name) => println!("Hello, {}!", name),
Err(e) => eprintln!("Error: {}", e),
}
}
Terminal output:
Enter your name: Alice
Hello, Alice!
input(comment: &str)Basic input function that prompts the user and returns the trimmed input.
use input_py::input;
// Basic usage
let name = input("What's your name")?;
println!("Hello, {}!", name);
// Empty prompt
let data = input("")?; // No prompt displayed
input_with_default(comment: &str, default: &str)Input with a default value that's used when the user enters nothing.
use input_py::input_with_default;
// With default value
let port = input_with_default("Enter port", "8080")?;
println!("Using port: {}", port);
// User sees: "Enter port [8080]:"
// Pressing Enter uses "8080"
input_trim(comment: &str, trim_whitespace: bool)Input with configurable whitespace trimming behavior.
use input_py::input_trim;
// Preserve whitespace
let raw_text = input_trim("Enter text", false)?;
println!("Raw: '{}'", raw_text);
// Trim whitespace (default behavior)
let clean_text = input_trim("Enter text", true)?;
println!("Clean: '{}'", clean_text);
All functions return Result<String, InputError> for robust error handling:
use input_py::{input, InputError};
match input("Enter something") {
Ok(value) => {
if value.is_empty() {
println!("Nothing entered!");
} else {
println!("You entered: {}", value);
}
}
Err(InputError::FlushError(e)) => {
eprintln!("Failed to flush stdout: {}", e);
}
Err(InputError::ReadError(e)) => {
eprintln!("Failed to read from stdin: {}", e);
}
}
use input_py::{input, input_with_default, input_trim};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== User Registration ===");
// Required field
let name = input("Full name")?;
if name.is_empty() {
eprintln!("Name is required!");
return Ok(());
}
// Optional field with default
let age = input_with_default("Age", "18")?;
// Preserve formatting for addresses
let address = input_trim("Address (with spacing)", false)?;
println!("\n--- Registration Complete ---");
println!("Name: {}", name);
println!("Age: {}", age);
println!("Address: '{}'", address);
Ok(())
}
Run the test suite:
cargo test
The library includes comprehensive tests covering:
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
input_with_default() function for default value supportinput_trim() function for configurable whitespace handlingInputError type (breaking change)