| Crates.io | input-lib |
| lib.rs | input-lib |
| version | 0.1.0 |
| created_at | 2025-08-14 03:03:03.40663+00 |
| updated_at | 2025-08-14 03:03:03.40663+00 |
| description | A basic library for reading input with prompts and parsing. |
| homepage | |
| repository | https://github.com/Phosphorus-M/input-lib |
| max_upload_size | |
| id | 1794378 |
| size | 11,121 |
A basic library for reading input with prompts and parsing.
This library provides a simple way to read input from the user, optionally displaying a prompt, and parsing the input into various types.
This library is part of a RFC process.
FromStr.use std::error::Error;
use input_lib::input;
fn main() -> Result<(), Box<dyn Error>> {
let name: String = input!("Enter your name: ")?;
println!("Hello, {}!", name);
Ok(())
}
#![allow(dead_code)]
use std::str::FromStr;
use input_lib::{input};
#[derive(Debug)]
struct Price {
currency: String,
amount: f64,
}
impl FromStr for Price {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.split_whitespace().collect();
if parts.len() != 2 {
return Err("String must have two parts".to_string());
}
let currency = parts[0].to_string();
let amount = parts[1].parse().unwrap();
Ok(Price { currency, amount })
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let price: Price = input!("Please enter a price: ")?;
// This could fail for example if the input is reading from a pipe and
// we delete the file whose descriptor is being read while the
// program is running
println!("{price:#?}!");
Ok(())
}
use input_lib::{input, InputError};
fn main() {
let number: i32 = match input!("Please enter a number: ") {
Ok(value) => value,
Err(e) => match e {
InputError::Eof => {
println!("End of input reached.");
return;
},
InputError::Parse(_) => {
println!("Failed to parse the input.");
return;
},
InputError::Io(_) => {
println!("An I/O error occurred.");
return;
},
},
};
println!("You entered: {}", number);
}