#Suggestion and feedback for my first script in Rust

14 messages · Page 1 of 1 (latest)

shut grove
#

There's one thing that is really problematic in your code, and that is that temperatures do not really fit into the integer degrees model, since 1°C is 33.8°F, so I'd change the signature of convert into fn convert(temperature: f64, unit: char) -> f64

#

apart from that, your code is actually not too bad for a first script 🙂

#

of course, it could be cleaner, so don't hesitate to rewrite it when you have a bit more experience in Rust

mortal cipher
#

I don't think input should need an underscore, as it's both used in the function, and returned as it's last statement, so that shouldn't produce any warnings...

#

Similarly in the case of the convert function, the return isn't nessacary, due to being the last statement in the function, it would return the value from the match statement implicitly...

quick steppe
#

Are you familiar with clippy? 😇

errant kayak
#

Your convert returns 0 on bad input, which isn't good, as it hides the error in a sentinel value
Your function should return a Result or an Option instead, and you can then handle that in the caller and print an error if it's invalid
Or alternately (I prefer this) you can make an enum for Celsius and Fahrenheit, making the concert function infallible. You'd match on the char outside and turn it into the enum, displaying an error if it's invalid

errant kayak
#

ah fine, the book should tell you more about it later ferristhumbsup

mortal cipher
#

I don't remember that??

#

It's the official Rust linter. It's installed as part of cargo, and will highlight elements of your code which it clarifies as being iffy...

mortal cipher
#

Ctrl + J

#

I think that's the binding to open the command pane...

#

Ohhh... I forgot about that...

#

Yeah, I think I was off, doing entirely my own shit... I completely forgot there were suggested exercises, alongside the walkthroughs...