How to get hashmap key values

I am making a hasmap in loop like this

      for players in 1..=num{
            println!("Enter the name of player # {}",players);
            io::stdin()
                  .read_line(&mut player_name)
                  .expect("Failed to read line");           
            scores.insert(&players,player_name.trim().to_string());
            player_name=String::from("");
        }

Now I want to retrieve the names of each player with respect to their keys and trying to get them by this code

  for players in 1..=num{
         let secret_number = rand::thread_rng().gen_range(1, 6);
//    total += secret_number;
  println!("Turn {} Dice Roll of Player {} - {} is {} ",turn,players,
                    scores.get(&players),secret_number);
   }

but the above code is giving error. Please help.

When asking for help, please make it easy for others to help you.

  1. Use code formatting when posting your code
  2. Make sure your code is a self-contained example that others can easily take and run, for example in the playground
  3. Be explicit about what doesn't work. Include any error message.

For your actual problem, you need to consider the case that the hashmap doesn't contain the key you asked for. See the documentation for more information about Option.

ok thanks I solved it

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.