In many real-world applications, converting temperatures between different units is a common task. For example, meteorologists, scientists, and engineers often need to convert temperatures from Celsius to Fahrenheit or Kelvin. In this challenge, you'll implement a function that converts temperatures between these units using logical operators and conditional statements.
Your task is to write a function convert_temperature
that takes three parameters: a float representing the temperature value, a string representing the unit of the input temperature, and another string representing the desired unit for the output temperature. The function should return a Result
type with either the converted temperature as a float or an error message if the input is invalid.
"Invalid unit"
.Ok
variant of the Result
.The Celsius and Fahrenheit scales are named after the scientists Anders Celsius and Daniel Gabriel Fahrenheit, respectively. The Kelvin scale, on the other hand, is named after Lord Kelvin, a British physicist. Unlike the Celsius and Fahrenheit scales, which are based on the freezing and boiling points of water, the Kelvin scale is an absolute temperature scale based on the concept of absolute zero, the lowest possible temperature where all molecular motion ceases.
F = C * (9/5) + 32
C = (F - 32) * (5/9)
K = C + 273.15
C = K - 273.15
kylenoteboom
const ALLOWED_UNITS: &str = "CFK";pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here if !ALLOWED_UNITS.contains(from_unit) { return Err("Invalid unit".to_string()); } if !ALLOWED_UNITS.contains(from_unit) { return Err("Invalid unit".to_string()); } if from_unit == "C" { if to_unit == "C" { return Err(format!("Invalid to_unit: {}", to_unit)); } if to_unit == "F" { return Ok((value * (9f64 / 5f64)) + 32.0); } if to_unit == "K" { return Ok(value + 273.15); } } if from_unit == "F" { if to_unit == "C" { return Ok((value - 32.0) * (5 / 9) as f64) } if to_unit == "F" { return Err(format!("Invalid to_unit: {}", to_unit)); } if to_unit == "K" { return Ok((value - 32.0) * (5f64 / 9f64) + 273.15); } } if from_unit == "K" { if to_unit == "C" { return Ok(value - 273.15); } if to_unit == "F" { return Ok((value - 273.15) * (9f64 / 5f64) + 32.0); } if to_unit == "K" { return Err(format!("Invalid to_unit: {}", to_unit)); } } Err("Invalid unit".to_string())}
martin-unit
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match from_unit { "C" => { match to_unit { "F" => { return Ok(value * (9 as f64/5 as f64) + 32 as f64); }, "K" => { return Ok(value + 273.15); }, _ => Err("Invalid unit".to_string()) } }, "F" => { match to_unit { "C" => { return Ok((value - 32 as f64) * (5/9) as f64); }, "K" => { return Ok((value - 32 as f64) * (5/9) as f64 + 273.15 as f64); }, _ => Err("Invalid unit".to_string()) } }, "K" => { match to_unit { "C" => { return Ok(value - 273.15 as f64); }, "F" => { return Ok((value - 273.15 as f64) * (9/5) as f64 + 32 as f64); }, _ => Err("Invalid unit".to_string()) } } _ => Err("Invalid unit".to_string()) }}
XtebanUy
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok(value * (9_f64/5_f64) + 32_f64), ("F", "C") => Ok((value - 32_f64) * (5_f64 / 9_f64)), ("C", "K") => Ok(value + 273.15_f64), ("K", "C") => Ok(value - 273.15_f64), ("K", "F") => convert_temperature(convert_temperature(value, "K", "C").unwrap(), "C", "F"), ("F", "K") => convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K"), ("C", "C") | ("F", "F") | ("K", "K") => Ok(value), _ => Err("Invalid unit".to_string()) }}
XtebanUy
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "C") | ("F", "F") | ("K", "K") => Ok(value), ("C", "F") => Ok(value * (9_f64/5_f64) + 32_f64), ("F", "C") => Ok((value - 32_f64) * (5_f64 / 9_f64)), ("C", "K") => Ok(value + 273.15_f64), ("K", "C") => Ok(value - 273.15_f64), ("K", "F") => convert_temperature(convert_temperature(value, "K", "C").unwrap(), "C", "F"), ("F", "K") => convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K"), _ => Err("Invalid unit".to_string()) }}
XtebanUy
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok(value * (9_f64/5_f64) + 32_f64), ("F", "C") => Ok((value - 32_f64) * (5_f64 / 9_f64)), ("C", "K") => Ok(value + 273.15_f64), ("K", "C") => Ok(value - 273.15_f64), ("K", "F") => convert_temperature(convert_temperature(value, "K", "C").unwrap(), "C", "F"), ("F", "K") => convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K"), _ => Err("Invalid unit".to_string()) }}
jimlawton
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "C") | ("F", "F") | ("K", "K") => Ok(value), ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("F", "K") => Ok((value - 32.0) * (5.0 / 9.0) + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * (9.0 / 5.0) + 32.0), (_, _) => Err(String::from("Invalid unit")), }}
devarajang
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match from_unit { "C" => { match to_unit { "K" => Ok(273.15 + value), "F" => Ok( value * 9.0 / 5.0 + 32.0 ), _ => Err("Invalid unit".to_string()) } }, "F" => { match to_unit { "K" => { let celcius = convert_temperature(value, "F", "C").unwrap() ; //Ok(273.15 + celcius) convert_temperature(celcius, "C", "K") }, "C" => Ok( ((value - 32.0) * 5.0)/9.0 ), _ => Err("Invalid unit".to_string()) } }, "K" => { match to_unit { "F" => { let celcius = value - 273.15; convert_temperature(celcius, "C", "F") }, "C" => Ok(value - 273.15), _ => Err("Invalid unit".to_string()) } }, _ => Err("Invalid unit".to_string()) }}
mbergkvist
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(c2f(value)), ("C", "K") => Ok(c2k(value)), ("F", "C") => Ok(f2c(value)), ("F", "K") => Ok(c2k(f2c(value))), ("K", "C") => Ok(k2c(value)), ("K", "F") => Ok(c2f(k2c(value))), _ => Err(String::from("Invalid unit")), }}fn f2c(value: f64) -> f64 { (value - 32_f64) * (5_f64/9_f64)}fn c2f(value: f64) -> f64 { value * (9_f64/5_f64) + 32_f64}fn c2k(value: f64) -> f64 { value + 273.15}fn k2c(value: f64) -> f64 { value - 273.15}
xiuchiliudu
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let cel = match from_unit { "C" => value, "F" => (value - 32.0) * (5.0 / 9.0), "K" => value - 273.15, _ => return Err("Invalid unit".to_string()), }; let result = match to_unit { "C" => cel, "F" => cel * 9.0 / 5.0 + 32.0, "K" => cel + 273.15, _ => return Err("Invalid unit".to_string()), }; Ok(result)}
DivineGod
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let celcius = match from_unit { "F" => (value - 32.0) * (5.0/9.0), "C" => value, "K" => value - 273.15, _ => return Err("Invalid unit".to_string()), }; let result = match to_unit { "F" => celcius * (9.0/5.0) + 32.0, "C" => celcius, "K" => celcius + 273.15, _ => return Err("Invalid unit".to_string()), }; Ok(result)}
mehdihmr
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok((value * 9.0 / 5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * 5.0 / 9.0), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), ("K", "F") => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()) }}
Algorab
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C","F") => Ok(value * (9_f64/5_f64) + 32_f64), ("F","C") => Ok((value - 32_f64) * (9_f64/5_f64)), ("C","K") => Ok(value + 273.15), ("K","C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) / 1.8 + 273.15), ("K", "F") => Ok((value - 273.15) * 1.8 + 32.0), _ => Err("Invalid unit".to_string()), }}
sroas
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here Ok(match (from_unit, to_unit) { ("C", "F") => value * 1.8 + 32.0, ("C", "K") => value + 273.15, ("K", "C") => value - 273.15, ("K", "F") => (value - 273.15) * 1.8 + 32.0, ("F", "C") => (value - 32.0) / 1.8, ("F", "K") => (value - 32.0) / 1.8 + 273.15, _ => return Err("Invalid unit".to_string()), })}
yoakemae
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here if from_unit == "C" { if to_unit == "K" { return Ok(value + 273.15); } else if to_unit == "F" { return Ok(value * (9.0/5.0) + 32.0); } } else if from_unit == "F" { let c: f64 = (value - 32.0) * (5.0/9.0); if to_unit == "K" { return Ok(c + 273.15); } else if to_unit == "C" { return Ok(c); } } else if from_unit == "K" { let c: f64 = value - 273.15; if to_unit == "F" { return Ok(c * (9.0/5.0) + 32.0); } else if to_unit == "C" { return Ok(c); } } return Err("Invalid unit".to_string());}
danielmpetrov
fn c2f(value: f64) -> f64 { value * (9.0 / 5.0) + 32.0}fn f2c(value: f64) -> f64 { (value - 32.0) * (5.0 / 9.0)}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(c2f(value)), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok(f2c(value)), ("F", "K") => Ok(f2c(value) + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok(c2f(value - 273.15)), _ => Err("Invalid unit".to_string()), }}
qiyuan711
fn c2f(value: f64) -> f64 { value * (9.0 / 5.0) + 32.0}fn f2c(value: f64) -> f64 { (value - 32.0) * (5.0 / 9.0)}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok(c2f(value)), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok(f2c(value)), ("F", "K") => Ok(f2c(value) + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok(c2f(value - 273.15)), _ => Err("Invalid unit".to_string()), }}
nt2311-vn
enum TUnit { C, F, K, Invalid}impl From<&str> for TUnit { fn from(value: &str) -> TUnit { match value { "C" => TUnit::C, "F" => TUnit::F, "K" => TUnit::K, _ => TUnit::Invalid } }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let from = TUnit::from(from_unit); let to = TUnit::from(to_unit); match (from, to) { (TUnit::Invalid, _) | (_, TUnit::Invalid) => Err(String::from("Invalid unit")), (TUnit::F, TUnit::C) => Ok((value - 32.0) * (5.0/9.0)), (TUnit::F, TUnit::K) => Ok((value - 32.0) * (5.0/9.0) + 273.15), (TUnit::C, TUnit::F) => Ok(value * (9.0 / 5.0) + 32.0), (TUnit::C, TUnit::K) => Ok(value + 273.15), (TUnit::K, TUnit::C) => Ok(value - 273.15), (TUnit::K, TUnit::F) => Ok((value -273.15) * (9.0 / 5.0) + 32.0), _ => Ok(value) } }// pub fn check_inout(from_unit: &str, to_unit: &str) -> bool {// match (from_unit, to_unit) {// ("C" | "K" | "F", "C" | "K" | "F") => true,// _ => false// }// }////// pub fn perform_convert(value: f64,from_unit: &str, to_unit: &str) -> f64 {// match (from_unit, to_unit) {// ("F", "C") => (value - 32.0) * (5.0/9.0) ,// ("F", "K") => (value - 32.0) * (5.0/9.0) + 273.15,// ("C", "F") => value * (9.0 / 5.0) + 32.0,// ("C", "K") => value + 273.15,// ("K", "C") => value - 273.15,// ("K", "F") => (value -273.15) * (9.0 / 5.0) + 32.0,// _ => value//// }// }
nt2311-vn
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if !check_inout(from_unit, to_unit) { Err(String::from("Invalid unit")) } else { Ok(perform_convert(value, from_unit, to_unit)) }}pub fn check_inout(from_unit: &str, to_unit: &str) -> bool { match (from_unit, to_unit) { ("C" | "K" | "F", "C" | "K" | "F") => true, _ => false }}pub fn perform_convert(value: f64,from_unit: &str, to_unit: &str) -> f64 { match (from_unit, to_unit) { ("F", "C") => (value - 32.0) * (5.0/9.0) , ("F", "K") => (value - 32.0) * (5.0/9.0) + 273.15, ("C", "F") => value * (9.0 / 5.0) + 32.0, ("C", "K") => value + 273.15, ("K", "C") => value - 273.15, ("K", "F") => (value -273.15) * (9.0 / 5.0) + 32.0, _ => value }}
nt2311-vn
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if !check_inout(from_unit, to_unit) { Err(String::from("Invalid unit")) } else { Ok(perform_convert(value, from_unit, to_unit)) }}pub fn check_inout(from_unit: &str, to_unit: &str) -> bool { match (from_unit, to_unit) { ("C" | "K" | "F", "C" | "K" | "F") => true, _ => false }}pub fn perform_convert(value: f64,from_unit: &str, to_unit: &str) -> f64 { match (from_unit, to_unit) { ("F", "C") => (value - 32.0) * (5.0/9.0) , ("F", "K") => (value - 32.0) * (5.0/9.0) + 273.15, ("C", "F") => value * (9.0 / 5.0) + 32.0, ("C", "K") => value + 273.15, ("K", "C") => value - 273.15, ("K", "F") => (value -273.15) * (9.0 / 5.0) + 32.0, _ => value }}
nt2311-vn
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if !check_inout(from_unit, to_unit) { Err(String::from("Invalid unit")) } else { Ok(perform_convert(value, from_unit, to_unit)) }}pub fn check_inout(from_unit: &str, to_unit: &str) -> bool { match (from_unit, to_unit) { ("C" | "K" | "F", "C" | "K" | "F") => true, _ => false }}pub fn perform_convert(value: f64,from_unit: &str, to_unit: &str) -> f64 { match (from_unit, to_unit) { ("F", "C") => (value - 32.0) * (5.0/9.0) , ("F", "K") => (value - 32.0) * (5.0/9.0) + 273.15, ("C", "F") => value * (9.0 / 5.0) + 32.0, ("C", "K") => value + 273.15, ("K", "C") => value - 273.15, ("K", "F") => (value -273.15) * (9.0 / 5.0) + 32.0, _ => value }}
nt2311-vn
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if !check_inout(from_unit, to_unit) { Err(String::from("Invalid unit")) } else { Ok(perform_convert(value, from_unit, to_unit)) }}pub fn check_inout(from_unit: &str, to_unit: &str) -> bool { match (from_unit, to_unit) { ("C" | "K" | "F", "C" | "K" | "F") => true, _ => false }}pub fn perform_convert(value: f64,from_unit: &str, to_unit: &str) -> f64 { match (from_unit, to_unit) { ("F", "C") => (value - 32.0) * (5.0/9.0) , ("F", "K") => (value - 32.0) * (5.0/9.0) + 273.15, ("C", "F") => value * (9.0 / 5.0) + 32.0, ("C", "K") => value + 273.15, ("K", "C") => value - 273.15, ("K", "F") => (value -273.15) * (9.0 / 5.0) + 32.0, _ => value }}
tinthid
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok((value * 9.0/5.0) + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) * 5.0/9.0), ("F", "K") => Ok(((value - 32.0) * 5.0/9.0) + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok(((value - 273.15) * 9.0/5.0) + 32.0), (from, to) if from == to => Ok(value), // Same unit conversion _ => Err(format!("Invalid unit")), }}
vipaware
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok(value * 1.8 + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) * 5.0 / 9.0), ("F", "K") => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * 1.8 + 32.0), ("C", "C") | ("K", "K") | ("F", "F") => Ok(value), _ => Err("Invalid unit".into()), }}
maxemontio
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if from_unit == "C" && to_unit == "F" { return Ok(value * (9f64/5f64) + 32f64); } else if from_unit == "F" && to_unit == "C" { return Ok((value - 32f64) * (5f64/9f64)); } else if from_unit == "C" && to_unit == "K" { return Ok(value + 273.15f64); } else if from_unit == "K" && to_unit == "C" { return Ok(value - 273.15f64); } else if from_unit == "F" && to_unit == "K" { return Ok((value - 32f64) * (5f64/9f64) + 273.15f64); } else if from_unit == "K" && to_unit == "F" { return Ok((value - 273.15f64) * (9f64/5f64) + 32f64); } else { return Err("Invalid unit".to_string()); }}
jon-a-miller
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> {match from_unit { "C" => match to_unit { "C" => {Ok(value)}, "F" => {Ok(value * 9.0 / 5.0 + 32.0)}, "K" => {Ok(value + 273.15)}, _ => Err("Invalid unit".into()) }, "F" => match to_unit{ "C" => {Ok((value - 32.0) * 5.0 / 9.0)}, "F" => {Ok(value)}, "K" => {Ok((value - 32.0) * 5.0 / 9.0 + 273.15)}, _ => Err("Invalid unit".into()) }, "K" => match to_unit { "C" => {Ok(value - 273.15)}, "F" => {Ok((value - 273.15) * 9.0 / 5.0 + 32.0)}, "K" => {Ok(value)}, _ => Err("Invalid unit".into()) } _ => Err("Invalid unit".into()) }}
DV-13
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * (9f64/5f64) + 32f64), ("C", "K") => Ok(value + 273.15f64), ("F", "C") => Ok((value - 32f64) * (5f64/9f64)), ("F", "K") => Ok((value - 32f64) * (5f64/9f64) + 273.15f64), ("K", "C") => Ok(value - 273.15f64), ("K", "F") => Ok((value - 273.15f64) * (9f64/5f64) + 32f64), _ => Err("Invalid unit".to_string()) }}
nichideropa
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok(value * (9.0/5.0) + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) * (5.0/9.0)), ("K", "C") => Ok(value - 273.15), ("K", "F") => { let celsius = value - 273.15; Ok(celsius * (9.0/5.0) + 32.0) } ("F", "K") => { let celsius = (value - 32.0) * (5.0/9.0); Ok(celsius + 273.15) } _ => Err("Invalid unit".to_string()), }}
Thymelizabeth
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here Ok(match (from_unit, to_unit) { ("C", "C") => value, ("C", "F") => c_to_f(value), ("C", "K") => c_to_k(value), ("F", "C") => f_to_c(value), ("F", "F") => value, ("F", "K") => c_to_k(f_to_c(value)), ("K", "C") => k_to_c(value), ("K", "F") => c_to_f(k_to_c(value)), ("K", "K") => value, _ => return Err(String::from("Invalid unit")) })}fn c_to_f(value: f64) -> f64 { value * (9. / 5.) + 32.}fn f_to_c(value: f64) -> f64 { (value - 32.) * (5. / 9.)}fn c_to_k(value: f64) -> f64 { value + 273.15}fn k_to_c(value: f64) -> f64 { value - 273.15}
LauriSarap
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => { let celsius = value - 273.15; let fahrenheit = convert_temperature(celsius, "C", "F")?; Ok(fahrenheit) } ("F", "K") => { let celsius = convert_temperature(value, "F", "C")?; Ok(celsius + 273.15) } _ => Err("Invalid unit".to_string()), }}
tamanishi
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok(value * (9 as f64 / 5 as f64) as f64 + 32 as f64), ("C", "K") => Ok(value + 273.15 as f64), ("F", "C") => Ok((value - 32 as f64) * (5/9) as f64), ("F", "K") => Ok((value - 32 as f64) * (5/9) as f64 + 273.15 as f64), ("K", "C") => Ok(value - 273.15 as f64), ("K", "F") => Ok((value - 273.15 as f64) * (9/5) as f64 + 32 as f64), _ => Err("Invalid unit".to_string()), }}
konishu
// use std::collections::HashSet;// pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> {// // TODO: Implement the function here// let units = HashSet::from(["K", "C", "F"]);// if units.get(from_unit).is_none() || units.get(to_unit).is_none() {// return Err("Invalid unit".to_string());// }// let c = match from_unit {// "K" => value - 273.15,// "F" => (value - 32.) * (5./9.),// _ => value,// };// let out = match to_unit {// "K" => c + 273.15,// "F" => c * (9./5.) + 32.,// _ => c,// };// Ok(out)// }use std::collections::HashSet;pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let units = HashSet::from(["K","C","F"]); if units.get(from_unit).is_none() || units.get(to_unit).is_none() { return Err("Invalid unit".to_string()); } let c = match from_unit { "K" => value - 273.15, "F" => (value - 32.) * (5./9.), _ => value, }; let out = match to_unit { "K" => c + 273.15, "F" => c * (9. / 5. ) + 32., _ => c, }; Ok(out)}
pynhpo
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * 1.8 + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) / 1.8), ("F", "K") => Ok((value - 32.0) / 1.8 + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => { let celcius = value - 273.15; Ok(celcius * (9.0 / 5.0) + 32.0) } _ => Err("Invalid unit".to_string()), }}
jeypiti
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let valid_units = ["C", "F", "K"]; if !valid_units.contains(&from_unit) || !valid_units.contains(&to_unit) { return Err("Invalid unit".to_string()) } let celsius = match from_unit { "C" => value, "F" => (value - 32.0) * (5.0 / 9.0), "K" => value - 273.15, _ => unreachable!() }; let res = match to_unit { "C" => celsius, "F" => celsius * (9.0/5.0) + 32.0, "K" => celsius + 273.15, _ => unreachable!() }; Ok(res)}
0xsmarter
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let valid_units = vec!["C", "F", "K"]; if !valid_units.contains(&from_unit) || !valid_units.contains(&to_unit) { return Err("Invalid unit".to_string()) } if from_unit == "C" { if to_unit == "C" { Ok(value) } else if to_unit == "F" { Ok(value * (9.0 / 5.0) + 32.0) } else { Ok(value + 273.15) } } else if from_unit == "F" { if to_unit == "C" { Ok((value - 32.0) * (5.0 / 9.0)) } else if to_unit == "F" { Ok(value) } else { Ok((value - 32.0) * (5.0 / 9.0) + 273.15) } } else { if to_unit == "C" { Ok(value - 273.15) } else if to_unit == "F" { Ok((value - 273.15) * (9.0 / 5.0) + 32.0) } else { Ok(value) } }}
aynugek
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "K") => Ok(value + 273.15), ("C", "F") => Ok(value * (9. / 5.) + 32.), ("F", "C") => Ok((value -32.) * (5. / 9.)), ("F", "K") => Ok(convert_f_to_k(value)), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok(convert_k_to_f(value)), (_, _) => Err("Invalid unit".to_string()), }}fn convert_f_to_k(value: f64) -> f64 { let c = convert_temperature(value, "F", "C").unwrap(); convert_temperature(c, "C", "K").unwrap()}fn convert_k_to_f(value: f64) -> f64 { let c = convert_temperature(value, "K", "C").unwrap(); convert_temperature(c, "C", "F").unwrap()}
tsucchinoko
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * 1.8 + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) / 1.8), ("F", "K") => Ok((value - 32.0) / 1.8 + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => { let celcius = value - 273.15; Ok(celcius * (9.0 / 5.0) + 32.0) } _ => Err("Invalid unit".to_string()), }}
ankeetparikh
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let valid_units = vec!["C", "F", "K"]; if !valid_units.contains(&from_unit) || !valid_units.contains(&to_unit) { return Err("Invalid unit".to_string()) } if from_unit == "C" { if to_unit == "C" { Ok(value) } else if to_unit == "F" { Ok(value * (9.0 / 5.0) + 32.0) } else { Ok(value + 273.15) } } else if from_unit == "F" { if to_unit == "C" { Ok((value - 32.0) * (5.0 / 9.0)) } else if to_unit == "F" { Ok(value) } else { Ok((value - 32.0) * (5.0 / 9.0) + 273.15) } } else { if to_unit == "C" { Ok(value - 273.15) } else if to_unit == "F" { Ok((value - 273.15) * (9.0 / 5.0) + 32.0) } else { Ok(value) } }}
maxvi
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => { let celcius = (value - 32.0) * (5.0 / 9.0); Ok(celcius + 273.15) }, ("K", "F") => { let celcius = value - 273.15; Ok(celcius * (9.0 / 5.0) + 32.0) } _ => Err("Invalid unit".to_string()), }}
whitwulf
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("F", "K") => Ok((value - 32.0) * (5.0 / 9.0) + 273.15), ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("C", "K") => Ok(value + 273.15), ("K", "F") => Ok((value - 273.15) * (9.0 / 5.0) + 32.0), ("K", "C") => Ok(value - 273.15), _ => Err("Invalid unit".to_string()), }}
whitwulf
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("F", "K") => Ok((value - 32.0) * (5.0 / 9.0) + 273.15), ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("C", "K") => Ok(value + 273.15), ("K", "F") => Ok((value - 273.15) * (9.0 / 5.0) + 32.0), ("K", "C") => Ok(value - 273.15), _ => Err("Invalid unit".to_string()), }}
Bubbet
enum Temperature { Celsius(f64), Fahrenheit(f64), Kelvin(f64),}impl Temperature { fn from_unit(value: f64, unit: &str) -> Result<Self, String> { match unit { "C" => Ok(Self::Celsius(value)), "F" => Ok(Self::Fahrenheit(value)), "K" => Ok(Self::Kelvin(value)), _ => Err("Invalid unit".into()) } } fn to_unit(&self, unit: &str) -> Result<Self, String> { match self { Self::Fahrenheit(n) if unit == "C" => Ok(Self::Celsius((n - 32.) * 5. / 9.)), Self::Celsius(n) if unit == "F" => Ok(Self::Fahrenheit(n * 9. / 5. + 32.)), Self::Fahrenheit(n) if unit == "K" => self.to_unit("C")?.to_unit("K"), Self::Celsius(n) if unit == "K" => Ok(Self::Kelvin(n + 273.15)), Self::Kelvin(n) if unit == "C" => Ok(Self::Celsius(n - 273.15)), Self::Kelvin(n) if unit == "F" => self.to_unit("C")?.to_unit("F"), _ => Err("Invalid unit".into()) } }}impl From<Temperature> for f64 { fn from(temp: Temperature) -> Self { match temp { Temperature::Celsius(n) => n, Temperature::Fahrenheit(n) => n, Temperature::Kelvin(n) => n, } }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here Ok(Temperature::from_unit(value, from_unit)?.to_unit(to_unit)?.into())}
StimhackSoftware
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let inter = match from_unit { "K" => value - 273.15, "C" => value, "F" => (value - 32.0) * 5.0 / 9.0, _x => { return Err("Invalid unit".to_string()) } }; let res = match to_unit { "K" => inter + 273.15, "C" => inter, "F" => inter * 9.0 / 5.0 + 32.0, _x => { return Err("Invalid unit".to_string())} }; Ok(res)}
Rolando1994
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let res:f64; match (from_unit, to_unit) { ("C", "F") => {res = value*(9.0/5.0)+32.0; Ok(res)}, ("F", "C") => {res = (value-32.0)*(5.0/9.0); Ok(res)}, ("K", "C") => {res = value-273.15; Ok(res)}, ("C", "K") => {res = value+273.15; Ok(res)}, ("F", "K") => {res = (value-32.0)*(5.0/9.0)+273.15; Ok(res)}, ("K", "F") => {res = (value-273.15)*(9.0/5.0)+32.0; Ok(res)}, _ => Err("Invalid unit".to_string()) }}
cloki0610
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok(value*(9.0/5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * (9.0/5.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) * (9.0/5.0) + 273.15), ("K", "F") => Ok((value-273.15)*(9.0/5.0) + 32.0), _ => Err("Invalid unit".to_string()) }}
lucas-heck
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value*(9.0/5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * (9.0/5.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) * (9.0/5.0) + 273.15), ("K", "F") => Ok((value-273.15)*(9.0/5.0) + 32.0), _ => Err("Invalid unit".to_string()) }}
nknknknkn2
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let valid_units = vec!["C", "F", "K"]; if valid_units.contains(&from_unit) == false || valid_units.contains(&to_unit) == false { return Err("Invalid unit".to_string()); } return match &from_unit { &"C" if to_unit == "F" => Ok(value * (9.0/5.0) + 32.0), &"C" if to_unit == "K" => Ok(value + 273.15), &"C" if to_unit == "C" => Ok(value), &"F" if to_unit == "F" => Ok(value), &"F" if to_unit == "K" => convert_temperature((value - 32.0) * (5.0/9.0), "C", "K"), &"F" if to_unit == "C" => Ok((value - 32.0) * (5.0/9.0)), &"K" if to_unit == "F" => convert_temperature(value - 273.15, "C", "F"), &"K" if to_unit == "K" => Ok(value), &"K" if to_unit == "C" => Ok(value - 273.15), &&_ => todo!() }}
josschne
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("F", "C") => Ok((value - 32.0) * (5.0/9.0)), ("C", "F") => Ok(value * (9.0/5.0) + 32.0), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) * (5.0/9.0) + 273.15), ("K", "F") => Ok((value - 273.15) * (9.0/5.0) + 32.0), _ => Err("Invalid unit".to_string()) }}
anthonycabreralara
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if from_unit == "C" && to_unit == "F" { Ok(value * (9.0/5.0) + 32.0) } else if from_unit == "C" && to_unit == "K" { Ok(value + 273.15) } else if from_unit == "F" && to_unit == "C" { Ok((value - 32.0) * (5.0/9.0)) } else if from_unit == "F" && to_unit == "K" { Ok(((value - 32.0) * (5.0/9.0)) + 273.15) } else if from_unit == "K" && to_unit == "C" { Ok(value - 273.15) } else if from_unit == "K" && to_unit == "F" { Ok((value - 273.15) * (9.0/5.0) + 32.0) } else { Err("Invalid unit".to_string()) }}
sergepin
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { const KELVIN_OFFSET: f64 = 273.15; const FAHRENHEIT_SCALE: f64 = 9.0 / 5.0; const INVERSE_FAHRENHEIT_SCALE: f64 = 5.0 / 9.0; const FAHRENHEIT_BASE: f64 = 32.0; match (from_unit, to_unit) { ("C", "F") => Ok(value * FAHRENHEIT_SCALE + FAHRENHEIT_BASE), ("C", "K") => Ok(value + KELVIN_OFFSET), ("K", "C") => Ok(value - KELVIN_OFFSET), ("K", "F") => Ok((value - KELVIN_OFFSET) * FAHRENHEIT_SCALE + FAHRENHEIT_BASE), ("F", "C") => Ok((value - FAHRENHEIT_BASE) * INVERSE_FAHRENHEIT_SCALE), ("F", "K") => Ok((value - FAHRENHEIT_BASE) * INVERSE_FAHRENHEIT_SCALE + KELVIN_OFFSET), _ => Err("Invalid unit".to_string()), }}
igroomgrim
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * (9f64/5f64) + 32f64), ("C", "K") => Ok(value + 273.15f64), ("F", "C") => Ok((value - 32f64) * (5f64/9f64)), ("F", "K") => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), ("K", "C") => Ok(value - 273.15f64), ("K", "F") => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()) }}