diff --git a/src/main.rs b/src/main.rs index 0e5826a35c4ef5ab299e3744e082bf21ac9269f3..baf03c44501f145b360230a9e66b74f081a35dd8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,13 +10,12 @@ fn main() { let secret_number = rand::thread_rng().gen_range(1, 101); - let mut tries_counter : u32 = 0; - let mut attempt_history = HashMap::new(); + let mut tries_counter: u32 = 0; + let mut attempt_history = HashMap::new(); println!("The secret number is: {}", secret_number); loop { - // Get the value from inside the Ok, else print the error let guess = match get_input() { Ok(num) => num, @@ -36,28 +35,28 @@ fn main() { match guess.cmp(&secret_number) { - Ordering::Less => println!("Too small!"), + Ordering::Less => println!("Too small!"), Ordering::Greater => println!("Too big!"), - Ordering::Equal => { + Ordering::Equal => { println!("You win!"); println!("Total number of attempts: {}", tries_counter); for x in attempt_history { println!("{:?}", x); } - println!("Since the HashMap does not push things onto a heap like -the Vec-type does, it will iterate over and print in the order items are found"); + println!( + "Since the HashMap does not push things onto a heap like +the Vec-type does, it will iterate over and print in the order items are found" + ); break; } } println!("Attempts so far: {}\n ", tries_counter); - } } fn get_input() -> Result<u32, String> { - let mut text_input = String::new(); println!("Please input your guess."); @@ -66,9 +65,8 @@ fn get_input() -> Result<u32, String> { Instead of using the expect, save and return potential error */ - let input = io::stdin() - .read_line(&mut text_input); - + let input = io::stdin().read_line(&mut text_input); + match input { Ok(_) => (), Err(e) => return Err(format!("{}", e)), @@ -80,4 +78,4 @@ fn get_input() -> Result<u32, String> { return Err(format!("in parsing u32, {}", e)); } }; -} \ No newline at end of file +}