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 !
#[derive(Debug)]
enum Temp{
Celsius(f32),
Fahrenheit(f32),
Kelvin(f32),
}
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);
println!("{:?}",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");
process::exit(0);
},
}
}
fn conv_temp(inp_temp: & Temp, tar_temp: &mut Temp){
*tar_temp = match inp_temp{
Temp::Celsius(c)=>{
match tar_temp{
Temp::Celsius(_) => Temp::Celsius(*c),
Temp::Fahrenheit(_) => Temp::Fahrenheit(1.8*(*c) + 32.0),
Temp::Kelvin(_) => Temp::Kelvin(*c + 273.15),
}
},
Temp::Kelvin(k)=>{
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),
}
},
Temp::Fahrenheit(f)=>{
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),
}
},
}
}