I am trying to get the last value of a string from user input. I tried implementing it but it does not give the expected output:
println!("Please input temp (ex: 60F): ");
let mut temp = String::new();
io::stdin()
.read_line(&mut temp)
.expect("Failed to read line");
println!("{temp}");
let unit = temp.chars().last().unwrap();
println!("unit: {}", unit);
> Please input temp (ex: 60F):
60f
unit:
I would've expected the last line to look something like:
unit: f
I tried multiple solutions from StackOverflow and reddit but it did not work as well.
Your string contains the \n
inputted by the user. try this
let unit = temp.trim().chars().last().unwrap();