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