Code - Review , Wanted to know if it s decently cleaner and consistent?

I have written a simple program for interconverting temp in celsius,kelvin and fahrenheit, i wanted feedback from you guys on if its fine or how i can make it more cleaner,consistent, more efficient and any other improvements,

since i don't have much experience in writing such detailed programs as in rust so wanted your feedback

Thank you in advance !


enum Temp{

use std::{io,process};

fn main() {
    let (mut key1 , mut key2,mut value) = (String::new(),String::new(),String::new());

    println!("please enter the inp temp without unit");

    io::stdin().read_line(& mut value).expect("Failed! to read input");

    let value :f32 = value.trim().parse().expect("please Enter a Valid Temp without unit");

    println!("please enter unit of the input Temp");

    println!("C -> Celsius\nK -> Kelvin\nF -> Fahrenheit");

    io::stdin().read_line(& mut key1).expect("Failed! to read input");

    let key1 = key1.trim().to_ascii_uppercase();
    let inp_temp = def_temp(&key1[..],&value);

    println!("please enter unit of the Target Temp");

    io::stdin().read_line(& mut key2).expect("Failed! to read input");

    let key2 = key2.trim().to_ascii_uppercase();
    let mut tar_temp = def_temp(&key2[..],&value);

    conv_temp(&inp_temp,&mut tar_temp);

fn def_temp(key: &str,temp_value: &f32) -> Temp{
     match key{
        "C" => Temp::Celsius(*temp_value),
        "F" => Temp::Fahrenheit(*temp_value), 
        "K" => Temp::Kelvin(*temp_value), 
         _ => {
             println!("please Enter Valid input");

fn conv_temp(inp_temp: & Temp, tar_temp: &mut Temp){
    *tar_temp = match inp_temp{

            match tar_temp{
                Temp::Celsius(_) => Temp::Celsius(*c),
                Temp::Fahrenheit(_) => Temp::Fahrenheit(1.8*(*c) + 32.0),
                Temp::Kelvin(_) => Temp::Kelvin(*c + 273.15),

            match tar_temp{
                Temp::Celsius(_) => Temp::Celsius(*k - 273.15),
                Temp::Fahrenheit(_) => Temp::Fahrenheit(1.8*(*k - 273.15) + 32.0),
                Temp::Kelvin(_) => Temp::Kelvin(*k),


            match tar_temp{
                Temp::Celsius(_) => Temp::Celsius((*f - 32.0)/1.8),
                Temp::Fahrenheit(_) => Temp::Fahrenheit(*f),
                Temp::Kelvin(_) => Temp::Kelvin((*f + 459.67)*0.5555),


I would discourage things like process::exit in "helper"-style functions like this. This would be a great place to apply Result-based error handling, and then write the error message (and perhaps exit) in main instead.

That way the function is usable in other places that might not want to kill the program if the input is bad.


Yeah ! Never thought about that ,letting main fn handle the error would be much better Thank you ! :grinning:

The formatting looks inconsistent, I recommend to run cargo fmt.

I would use other variable names than key1 or value, they are too generic and don't communicate well what they are about.

Instead of using "{:?}" (debug formatting) to output the result I would implement std::fmt::Display for your enum, so you can use "{}".

1 Like

thank you for the feedback.
making those changes now !

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.