cl_utils/
lib.rs

1use std::io::stdin;
2
3const READ_ERROR: &'static str = "Error: unable to read user input";
4
5/// Reads a u8 from stdin and returns a u8. Panics on fail.
6pub fn read_u8() -> u8
7{
8    let mut input = String::new();
9    stdin().read_line(&mut input).expect(READ_ERROR);
10    
11    input.trim().parse::<u8>().expect("Unable to parse input to a u8")
12
13}
14
15/// Reads a line of user input from stdin into a String and returns it. Panics on fail.
16pub fn read_line() -> String
17{
18    let mut input = String::new();
19    stdin().read_line(&mut input).expect(READ_ERROR);
20    
21    input
22}
23
24/// Prompts the user with the provided &str msg before calling read_line() to get a user input string
25pub fn read_line_prompt(msg: &str) -> String
26{
27    println!("{}", msg);
28    read_line()
29}