I’m in the very early days of learning how to code with Rust. I’ve started with a CLI (command line interface) program that converts celsius temperatures to fahrenheit temperatures.
This evening, I had a lovely little breakthrough. I was not clear, even after working through the Guessing Game example in “The Book” of all things Rust, how to convert user input from a string to a number. The user input needed to be a number so that it could be operated on. I searched and searched…all the while thinking there must be some essential piece I’m not seeing.
I went back to “The Book”. Sure enought, it was there…shadowing variables.
Take the input then use the notion of shadowing variables to re-cast the string value into a number value.
Here’s the code:
let celsius: f32 = celsius.trim(().parse().expect("Please type a number!");
Suddenly, my little bit of fluff code was working as it needed to. This little epiphany may have come obviously to some, but a lot of the time obvious things escape me and I need to be hit over the head.
Feeling good! I can move on thorugh the rest of “The Book”.