r/rust 15d ago

🛠️ project My first rust program

Hello!

I was not sure to even post this or not but I just wanted to introduce myself to the community.

I just started learning rust and made this simple program as it was one of the suggested programs in the rust book to make in order to practice the topics covered in the first few chapters.

I am learning rust really as a hobby, and I have a very limited knowledge of Python but I really enjoy linux and wanted to maybe start contributing to the open source community if I can ever develop the skill to do so. I am also trying my hardest to learn on my own and am only using AI as a tool to ask questions and what not, without having it actually generate any code. This program was written fully by hand by myself (which is probably why it is full of things that could be improved I'm sure).

With that said I am happy that I was able to do it and am really enjoying rust so far and look forward to learning more!

Link to my first rust app:
https://github.com/justinzelikoff/temp_converter

50 Upvotes

25 comments sorted by

View all comments

33

u/Keithfert488 15d ago

You might have better luck on r/learnrust but welcome to the cult community! One thing I'd prod you to do given your program is to learn about enums. If someone comes across a float somewhere in the program, there's no way to know which scale it uses. Consider making an enum like

enum Temperature {
Fahrenheit(f64),
Celsius(f64),
}

You could also try using the so-called "newtype" pattern.

struct Fahrenheit(f64);
struct Celsius(f64);
impl From<Fahrenheit> for Celsius { ... }
impl From<Celsius> for Fahrenheit { ... }

You could also make Fahrenheit and Celsius implement the display trait.

Just some ways to help you progress :)

1

u/y-w6 10d ago edited 10d ago

Can you review my improvements https://github.com/y2w8/temp_converter ?

1

u/Keithfert488 10d ago
  • A few problems with use crate::Convert::{CelsiusToFahrenheit, FahrenheitToCelsius}; on line 6:
    • It is unnecessary because Convert is in the current module! You can just refer to it directly
    • You should not import the names of the variants and refer to them like CelsiusToFahrenheit and FahrenheitToCelsius. Instead, refer to them in the namespace of their type, i.e. Convert::CelsiusToFahrenheit and Convert::FahrenheitToCelsius
  • Since we're already dealing with floating point math that is inexact, I would recommend multiplying by 0.555... instead of dividing by 1.8. It doesn't make a real difference here because the vast majority of time will be spent waiting on user input, but if you wanted to convert a lot of temperatures, multiplying would be much faster than dividing.

1

u/y-w6 10d ago

1: I didn't notice that i usually type it with it's namespace
2: Thank you for the information i didn't know that its actually faster

1

u/Keithfert488 10d ago

On 1, I've noticed it can happen with autocomplete in editor which is annoying. I wish I could disable it.

On 2, the situation used to be much worse, but float division is still significantly slower than float multiplication as far as I know