Unable to trim string in loop

I am inserting a key as string in HashMAp. This key is taken as input from user in a loop but I am unable to trim the trailing \r\n. Please guide.
Here is my code:
for players in 1..=num{
println!("Enter the name of player # {}",players);
io::stdin()
.read_line(&mut player_name)
.expect("Failed to read line");

            let secret_number = rand::thread_rng().gen_range(1, 6);
            scores.insert(player_name.to_string(),secret_number);
            player_name=String::from("");
        }

when I give .trim() as scores.insert(player_name.to_string().trim() ,secrt_number) it gives the following error

--> src\main.rs:25:27
|
25 | scores.insert(player_name.to_string().trim(),secret_number);
| ^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
...
28 | println!("HashMap is{:#?}",scores);
| ------ borrow later used here
|
= note: consider using a let binding to create a longer lived value

use std::io;
use std::collections::HashMap;

use rand::Rng;

fn main() {
    let mut scores = HashMap::new();
    let num = 2;
    for players in 1..=num {
        println!("Enter the name of player # {}",players);
        let mut player_name = String::new();
        io::stdin()
            .read_line(&mut player_name)
            .expect("Failed to read line");
        let secret_number = rand::thread_rng().gen_range(1, 6); 
        scores.insert(player_name.trim().to_string(), secret_number);
    }
    println!("{:?}", scores);
}

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.