stdin_helper/lib.rs
1//! Simplify read_line method
2
3use std::{io::{self, Write}, str::FromStr};
4
5/// Ask user for a number until a valid number is detected
6pub fn get_number<T: FromStr>(msg: &str) -> T {
7 let mut input_buffer = String::new();
8
9 loop {
10 input_buffer.clear();
11
12 print!("{msg}");
13 io::stdout().flush().expect("Error on flush");
14
15 io::stdin().read_line(&mut input_buffer).expect("Error reading line");
16
17 match input_buffer.trim().parse::<T>() {
18 Ok(value) => return value,
19
20 _ => continue
21 }
22 }
23}