| Crates.io | input-handle |
| lib.rs | input-handle |
| version | 1.0.4 |
| created_at | 2025-01-25 18:33:48.068127+00 |
| updated_at | 2025-01-25 18:33:48.068127+00 |
| description | Simple library to handle input of any type |
| homepage | |
| repository | https://github.com/TT1882/import-handler |
| max_upload_size | |
| id | 1530766 |
| size | 9,750 |
Import-handler is a basic library designed to handle user inputs of any type and avoid a panic. It has error handling built in
get_u8_input(message)
Where message is what the user will be asked
Each function follows the same principles, adjusting for each data type. Here is the code for get_u32_input:
pub fn get_u32_input(message: &str) -> u32 {
println!("{}", message);
let mut userInput = String::new();
loop {
io::stdin().read_line(&mut userInput).expect("Failed to read line");
match userInput.trim().parse::<u32>() {
Ok(value) => {return value;}
Err(e) => {
println!("Invalid input! Error: {}", e);
userInput = String::new();
}
}
}
}
The function takes in &str data "message", which is the message displayed before the input. The input is then read and checked if it can be converted to u32. If it can, the value is returned, if not an error is displayed and the input variable is reset then a new input is gathered.