diff --git a/src/main.rs b/src/main.rs index d8dc771684d659cec43cfd6b02f9bf521dbdf9c6..0e5826a35c4ef5ab299e3744e082bf21ac9269f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ extern crate rand; use std::io; -use std::vec::Vec; +use std::collections::HashMap; use std::cmp::Ordering; use rand::Rng; @@ -11,7 +11,7 @@ fn main() { let secret_number = rand::thread_rng().gen_range(1, 101); let mut tries_counter : u32 = 0; - let mut attempt_history = Vec::<(u32, String)>::new(); + let mut attempt_history = HashMap::new(); println!("The secret number is: {}", secret_number); @@ -31,7 +31,9 @@ fn main() { tries_counter += 1; - attempt_history.push((tries_counter, format!("{}", guess))); + //attempt_history.push((tries_counter, format!("{}", guess))); + attempt_history.insert(tries_counter, format!("{}", guess)); + match guess.cmp(&secret_number) { Ordering::Less => println!("Too small!"), @@ -39,16 +41,11 @@ fn main() { Ordering::Equal => { println!("You win!"); println!("Total number of attempts: {}", tries_counter); - println!("Last entry first, maximum 3:"); - for (i, x) in attempt_history.iter().rev().enumerate() { + for x in attempt_history { println!("{:?}", x); - // Only print the 3 last (see .rev() above) - if i >= 2 { - break; - } - - } + 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; } }