Created
June 5, 2020 23:09
-
-
Save tolumide-ng/97289df94fe4a20b52469835ee9c7413 to your computer and use it in GitHub Desktop.
Convert temperatures between Fahrenheit and Celsius.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub mod temps { | |
use std::io; | |
pub fn calc_temp() -> () { | |
println!("Please enter a temperature you widh to convert"); | |
println!("Expected format: 32C ==> to convert 32C to F"); | |
println!("Expected format: 68F ==> to convert 32F to C"); | |
let mut value_to_convert = String::new(); | |
io::stdin() | |
.read_line(&mut value_to_convert) | |
.expect("Please enter a valid value to convert"); | |
let value_to_convert = value_to_convert.trim(); | |
let (temp, current_unit) = value_to_convert.split_at(&value_to_convert.len() - 1); | |
let val: u32 = temp.parse().expect("Please use this format 21C or 17F"); | |
let result: f64 = match current_unit.to_lowercase().as_str() { | |
"c" => ((val * 9 / 5) + 32) as f64, | |
"f" => ((val - 32) * 5 / 9) as f64, | |
_ => val as f64, | |
}; | |
println!("{}", result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment