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.
let result = convert_temperature(100.0, "C", "F");
assert_eq!(result, Ok(212.0));
let result = convert_temperature(32.0, "F", "C");
assert_eq!(result, Ok(0.0));
let result = convert_temperature(0.0, "C", "K");
assert_eq!(result, Ok(273.15));
let result = convert_temperature(300.0, "K", "C");
assert_eq!(result, Ok(26.85));
let result = convert_temperature(100.0, "C", "X");
assert_eq!(result, Err("Invalid unit".to_string()));
F = C * (9/5) + 32
C = (F - 32) * (5/9)
K = C + 273.15
C = K - 273.15
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let k = match from_unit { "K" => Ok::<f64, String>(value), "C" => Ok::<f64, String>(value + 273.15), "F" => Ok::<f64, String>((value - 32.) * 5. / 9. + 273.15), _ => Err("Invalid unit".into()), }?; match to_unit { "K" => Ok(k), "C" => Ok(k - 273.15), "F" => Ok((k - 273.15) * 9. / 5. + 32.), _ => Err("Invalid unit".into()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let k = match from_unit { "K" => Ok::<f64, String>(value), "C" => Ok::<f64, String>(value + 273.15), "F" => Ok::<f64, String>((value - 32.) * 5. / 9. + 273.15), _ => Err("Invalid unit".into()), }?; match to_unit { "K" => Ok(k), "C" => Ok(k - 273.15), "F" => Ok((k - 273.15) * 9. / 5. + 32.), _ => Err("Invalid unit".into()), }}
fn c2f(value: f64) -> f64 { value * (9f64 / 5f64) + 32_f64}fn c2k(value: f64) -> f64 { value + 273.15}fn k2c(value: f64) -> f64 { value - 273.15}fn f2c(value: f64) -> f64 { (value - 32_f64) * (5_f64 / 9_f64)}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "C")| ("K", "K") | ("F", "F") => Ok(value), ("C", "F") => Ok(c2f(value)), ("C", "K") => Ok(c2k(value)), ("K", "C") => Ok(k2c(value)), ("K", "F") => Ok(c2f(k2c(value))), ("F", "C") => Ok(f2c(value)), ("F", "K") => Ok(c2k(f2c(value))), _ => return Err("Invalid unit".to_string()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "C")| ("K", "K") | ("F", "F") => Ok(value), ("C", "F") => Ok(value*(9 as f64/5 as f64) + 32.0), ("F", "C") => Ok( (value-32.0) as f64 * (5.0/9.0) ), ("K", "C") => Ok(value as f64 - 273.15), ("C", "K") => Ok(value as f64 + 273.15), ("K", "F") => Ok( (value as f64 - 273.15)*(9 as f64/5 as f64) + 32.0 ), ("F", "K") => Ok( ( (value-32.0) as f64 * (5.0/9.0)) + 273.15 ), _ => Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let k = match from_unit { "K" => Ok::<f64, String>(value), "C" => Ok::<f64, String>(value + 273.15), "F" => Ok::<f64, String>((value - 32.) * 5. / 9. + 273.15), _ => Err("Invalid unit".into()), }?; match to_unit { "K" => Ok(k), "C" => Ok(k - 273.15), "F" => Ok((k - 273.15) * 9. / 5. + 32.), _ => Err("Invalid unit".into()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here if (from_unit != "C" && from_unit !="F" && from_unit != "K") || (to_unit != "C" && to_unit !="F" && to_unit != "K") { Err("Invalid unit".to_string()) } else { let formula = match from_unit { "C" => match to_unit { "F" => (value*9.0)/5.0+32.0, "K" => value + 273.15, _ => panic!("impossible") }, "F" => match to_unit { "C" => (value-32.0)*5.0/9.0, "K" => (value-32.0)*5.0/9.0+273.15, _ => panic!("impossible") }, "K" => match to_unit { "C" => value-273.15, "F" => (value-273.15)*9.0/5.0+32.0, _ => panic!("impossible") }, _ => panic!("impossible") }; Ok(formula) } }
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") => 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()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here return 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), (_, _) => Err("Invalid unit".to_string()) }}
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 * 1.8 + 32.), ("F", "C") => Ok((value - 32.) * (5. / 9.)), ("F", "K") => Ok(((value + 459.67) * 5.) / 9.), ("K", "F") => Ok((value * 1.8 - 459.67).round()), ("K", "C") => Ok(value - 273.15), _ if from_unit == to_unit => Ok(value), _ => Err("Invalid unit".to_owned()), }}
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()); } if from_unit == to_unit { return Ok(value); // no conversion needed } // Convert to Celsius first let value_in_celsius = match from_unit { "C" => value, "F" => (value - 32.0) * (5.0 / 9.0), "K" => value - 273.15, _ => unreachable!(), }; // Convert from Celsius to target unit let converted = match to_unit { "C" => value_in_celsius, "F" => value_in_celsius * (9.0 / 5.0) + 32.0, "K" => value_in_celsius + 273.15, _ => unreachable!(), }; Ok(converted)}
pub enum TempUnit { Celcius, Fahrenheit, Kelvin,}impl TempUnit { fn from_str(unit: &str) -> Result<Self, String> { match unit.as_bytes() { b"C" => Ok(Self::Celcius), b"F" => Ok(Self::Fahrenheit), b"K" => Ok(Self::Kelvin), _ => Err("Invalid unit".to_string()), } } fn to_celcius(&self, temp: f64) -> f64 { match self { Self::Celcius => temp, Self::Fahrenheit => (temp - 32.0) * 5.0 / 9.0, Self::Kelvin => temp - 273.15, } } fn from_celcius(&self, celcius: f64) -> f64 { match self { Self::Celcius => celcius, Self::Fahrenheit => celcius * 9.0 / 5.0 + 32.0, Self::Kelvin => celcius + 273.15, } }}pub fn convert_temperature(temp: f64, from: &str, to: &str) -> Result<f64, String> { if from == to { return Ok(temp); } let from_unit = TempUnit::from_str(from)?; let to_unit = TempUnit::from_str(to)?; let celcius = from_unit.to_celcius(temp); Ok(to_unit.from_celcius(celcius))}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let value = match from_unit { "C" => { match to_unit { "C" => value, "F" => value * (9.0/5.0) + 32.0, "K" => value + 273.15, _ => return Err("Invalid unit".to_string()) } }, "F" => { match to_unit { "C" => (value - 32.0) * (5.0/9.0), "F" => value, "K" => convert_temperature(value, "F", "C").unwrap() + 273.15, _ => return Err("Invalid unit".to_string()) } }, "K" => { match to_unit { "C" => value - 273.15, "F" => convert_temperature(value - 273.15, "C", "F").unwrap(), "K" => value, _ => return Err("Invalid unit".to_string()) } }, _ => return Err("Invalid unit".to_string()) }; Ok(value)}
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), ("F", "C") => Ok((value - 32f64) * (5f64 / 9f64)), ("K", "C") => Ok(value - 273.15), ("C", "K") => Ok(value + 273.15), ("F", "K") => Ok((value - 32f64) * (5f64 / 9f64) + 273.15), ("K", "F") => Ok((value - 273.15) * (9f64 / 5f64) + 32f64), (_, _) => Err("Invalid unit".to_string()), }}
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./5.) + 32.), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.) * (5./9.)), ("F", "K") => Ok(convert_temperature(value, "F", "C").unwrap() + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok(convert_temperature(value, "K", "C").unwrap() * (9./5.) + 32.), _ => Err("Invalid unit".to_string()), }}
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./5.) + 32.), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.) * (5./9.)), ("F", "K") => Ok(convert_temperature(value, "F", "C").unwrap() + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok(convert_temperature(value, "K", "C").unwrap() * (9./5.) + 32.), _ => Err("Invalid unit".to_string()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let from_idx = match from_unit { "C" => 0, "F" => 1, "K" => 2, _ => return Err("Invalid unit".into()), }; let to_idx = match to_unit { "C" => 0, "F" => 1, "K" => 2, _ => return Err("Invalid unit".into()), }; // same unit check if from_idx == to_idx { return Ok(value); } // direct conversion table [from][to] let result = match (from_idx, to_idx) { (0, 1) => value * 9.0/5.0 + 32.0, // C to F (0, 2) => value + 273.15, // C to K (1, 0) => (value - 32.0) * 5.0/9.0, // F to C (1, 2) => (value - 32.0) * 5.0/9.0 + 273.15, // F to K (2, 0) => value - 273.15, // K to C (2, 1) => (value - 273.15) * 9.0/5.0 + 32.0, // K to F _ => unreachable!(), }; Ok(result)}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let from_idx = match from_unit { "C" => 0, "F" => 1, "K" => 2, _ => return Err("Invalid unit".into()), }; let to_idx = match to_unit { "C" => 0, "F" => 1, "K" => 2, _ => return Err("Invalid unit".into()), }; // same unit check if from_idx == to_idx { return Ok(value); } // direct conversion table [from][to] let result = match (from_idx, to_idx) { (0, 1) => value * 9.0/5.0 + 32.0, // C to F (0, 2) => value + 273.15, // C to K (1, 0) => (value - 32.0) * 5.0/9.0, // F to C (1, 2) => (value - 32.0) * 5.0/9.0 + 273.15, // F to K (2, 0) => value - 273.15, // K to C (2, 1) => (value - 273.15) * 9.0/5.0 + 32.0, // K to F _ => unreachable!(), }; Ok(result)}
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()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let c_to_f = |c| c * (9.0/5.0) + 32.0; let f_to_c = |f| (f - 32.0) * (5.0/9.0); let c_to_k = |c| c + 273.15; let k_to_c = |k| k - 273.15; match (from_unit, to_unit) { ("C", "C") => Ok(value), ("F", "F") => Ok(value), ("K", "K") => Ok(value), ("C", "F") => Ok(c_to_f(value)), ("C", "K") => Ok(c_to_k(value)), ("F", "C") => Ok(f_to_c(value)), ("F", "K") => Ok(c_to_k(f_to_c(value))), ("K", "C") => Ok(k_to_c(value)), ("K", "F") => Ok(c_to_f(k_to_c(value))), _ => Err("Invalid unit".to_string()) }}
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)), ("F", "K") => Ok(convert_temperature(value, from_unit, "C").unwrap() + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok(convert_temperature(value, from_unit, "C").unwrap() * (9.0 / 5.0) + 32.0), (_,_) =>Err("Invalid unit".to_string()), }}
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)), ("F", "K") => Ok(convert_temperature(value, from_unit, "C").unwrap() + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok(convert_temperature(value, from_unit, "C").unwrap() * (9.0 / 5.0) + 32.0), (_,_) =>Err("Invalid unit".to_string()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let c = match from_unit { "C" => value, "F" => (value - 32.0) * 5.0 / 9.0, "K" => value - 273.15, _ => return Err("Invalid unit".into()) }; match to_unit { "C" => Ok(c), "F" => Ok(c * 9.0 / 5.0 + 32.0), "K" => Ok(c + 273.15), _ => Err("Invalid unit".into()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let c = match from_unit { "C" => value, "F" => (value - 32.0) * 5.0 / 9.0, "K" => value - 273.15, _ => return Err("Invalid unit".into()) }; match to_unit { "C" => Ok(c), "F" => Ok(c * 9.0 / 5.0 + 32.0), "K" => Ok(c + 273.15), _ => Err("Invalid unit".into()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let mut value = value; match from_unit { "K" => value -= 273.15, "C" => { }, "F" => { value -= 32.0; value *= 5.0/9.0; } _ => { return Err(String::from("Invalid unit")); } } match to_unit { "K" => Ok(value + 273.15), "C" => Ok(value), "F" => { value *= 9.0/5.0; Ok(value + 32.0) } _ => Err(String::from("Invalid unit")) }}
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".to_string()) }, "F" => match to_unit { "C" => Ok((value - 32.0) * (5.0/9.0)), "F" => Ok(value), "K" => convert_temperature(convert_temperature(value, "F", "C")?, "C", "K"), _ => Err("Invalid unit".to_string()) }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => convert_temperature(convert_temperature(value, "K", "C")?, "C", "F"), "K" => Ok(value), _ => Err("Invalid unit".to_string()) }, _ => Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function her 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), ("K", "C") => Ok(value - 273.15), ("C", "K") => Ok(value + 273.15), ("K", "F") => Ok((value - 273.15) * (9.0/5.0) + 32.0), ("F", "K") => Ok((value - 32.0) * (5.0/9.0) + 273.15), _ => Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let k = match from_unit { "C" => Ok(value + 273.15), "K" => Ok(value), "F" => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), _ => Err("Invalid unit".to_string()) }?; match to_unit { "C" => Ok(k - 273.15), "K" => Ok(k), "F" => Ok((k - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()) }}
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) * (5_f64/9_f64)), ("C", "K") => Ok(value + 273.15_f64), ("K", "C") => Ok(value - 273.15_f64), ("F", "K") => convert_temperature(convert_temperature(value, "F", "C")?, "C", "K"), ("K", "F") => convert_temperature(convert_temperature(value, "K", "C")?, "C", "F"), _ => Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here if (from_unit != "C" && from_unit != "F" && from_unit != "K") || (to_unit != "C" && to_unit != "F" && to_unit != "K") { return Err(String::from("Invalid unit")); } match from_unit { "C" => { if to_unit == "F" { Ok(value * (9 as f64 / 5 as f64) + 32 as f64) } else { Ok(value + 273.15) } }, "F" => { if to_unit == "C" { Ok((value - 32 as f64) * (5 as f64 / 9 as f64)) } else { Ok((value - 32 as f64) * (5 as f64 / 9 as f64) + 273.15) } }, "K" => { if to_unit == "C" { Ok(value - 273.15) } else { Ok((value - 273.15) * (9 as f64 / 5 as f64) + 32 as f64) } } _ => Err(String::from("Invalid unit")), }}
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), ("K", "C") => Ok(value - 273.15), ("C", "F") => Ok(value * (9f64/5f64) + 32f64), ("F", "C") => Ok((value - 32f64) * (5f64/9f64)), ("F", "K") => { let result = convert_temperature(value, "F", "C"); match result { Ok(val) => convert_temperature(val, "C", "K"), err => err } }, ("K", "F") => { let result = convert_temperature(value, "K", "C"); match result { Ok(val) => convert_temperature(val, "C", "F"), err => err } } _ => Err("Invalid unit".to_string()) }}
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") => 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()), }}
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") => Ok(convert_temperature(value, "F", "C").unwrap() + 273.15), ("K", "F") => Ok(convert_temperature(value-273.15, "C", "F").unwrap()), _ => Err("Invalid unit".to_string()) }}
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".to_string()) } }, "F" => { match to_unit { "F" => Ok(value), "C" => Ok((value - 32.0) * (5.0/9.0)), "K" => Ok((value - 32.0) * (5.0/9.0) + 273.15), _ => Err("Invalid unit".to_string()) } }, "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".to_string()) } }, _ => Err("Invalid unit".to_string()) }}// Ugh, i didn't realize you can make a tuple to match 2 things at once
pub fn convert_temperature(val: 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(val), ("C", "F") => Ok( (val * (9. / 5.) + 32.) ), ("F", "C") => Ok((val - 32.) * (5./9.)), ("C", "K") => Ok(val + 273.15), ("K", "C") => Ok(val - 273.15), ("F", "K") => Ok((val - 32.) * 5. / 9. + 273.15), ("K", "F") => Ok((val - 273.15) * 9. / 5. + 32.), _ => Err("Invalid unit".to_string()), }}
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. / 5. + 32.), ("F", "C") => Ok((value - 32.) * 5. / 9.), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.) * 5. / 9. + 273.15), ("K", "F") => Ok((value - 273.15) * 9. / 5. + 32.), _ => Err("Invalid unit".to_string()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let res = match (from_unit, to_unit) { ("C", "F") => value * (9.0/5.0) + 32.0, ("F", "C") => (value - 32.0) * (5.0/9.0), ("C", "K") => value + 273.15, ("K", "C") => value - 273.15, ("K", "F") => (value - 273.15) * (9.0/5.0) + 32.0, ("F", "K") => (value - 32.0) * (5.0/9.0) + 273.15, _ => return Err(String::from("Invalid unit")) }; Ok(res)}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit.to_lowercase().as_str(), to_unit.to_lowercase().as_str()) { ("c", "f") => Ok(value * (9.0 / 5.0) + 32.0), // Celsius to Fahrenheit ("f", "c") => Ok((value - 32.0) * (5.0 / 9.0)), // Fahrenheit to Celsius ("c", "k") => Ok(value + 273.15), // Celsius to Kelvin ("k", "c") => Ok(value - 273.15), // Kelvin to Celsius ("f", "k") => Ok((value - 32.0) * (5.0 / 9.0) + 273.15), // Fahrenheit to Kelvin ("k", "f") => Ok((value - 273.15) * (9.0 / 5.0) + 32.0), // Kelvin to Fahrenheit _ => Err(String::from("Invalid unit")), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "C") | ("F", "F") | ("K", "K") => Ok(value), ("C", "F") => Ok(value * 9. / 5. + 32.), ("F", "C") => Ok((value - 32.) * 5. / 9.), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.) * 5. / 9. + 273.15), ("K", "F") => Ok((value - 273.15) * 9. / 5. + 32.), _ => Err("Invalid unit".to_string()), }}
fn to_celsius(value: f64, from_unit: &str) -> Result<f64, String> { match from_unit { "K" => Ok(value - 273.15), "F" => Ok((value - 32.0) * (5./9.)), "C" => Ok(value), _ => Err("Invalid unit".into()) }}fn to_fahrenheit(value: f64, from_unit: &str) -> Result<f64, String> { match from_unit { "K" => Ok((value - 273.15) * (9./5.) + 32.0), "F" => Ok(value), "C" => Ok(value * (9./5.) + 32.0), _ => Err("Invalid unit".into()) }}fn to_kelvin(value: f64, from_unit: &str) -> Result<f64, String> { match from_unit { "K" => Ok(value), "F" => Ok((value - 32.0) * (5./9.) + 273.15), "C" => Ok(value + 273.15), _ => Err("Invalid unit".into()) }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match to_unit { "K" => to_kelvin(value, from_unit), "F" => to_fahrenheit(value, from_unit), "C" => to_celsius(value, from_unit), _ => Err("Invalid unit".into()) }}
const INVALID_INPUT_ERROR_STR: &'static str = "Invalid unit";const CELSIUS_CHAR_STR: &'static str = "C";const FAHRENHEIT_CHAR_STR: &'static str = "F";const KELVIN_CHAR_STR: &'static str = "K";#[derive(Debug, Copy, Clone, PartialEq)]pub struct Celsius(pub f64);#[derive(Debug, Copy, Clone, PartialEq)]pub struct Fahrenheit(pub f64);#[derive(Debug, Copy, Clone, PartialEq)]pub struct Kelvin(pub f64);impl Celsius { pub fn to_fahrenheit(self) -> Fahrenheit { Fahrenheit(self.0 * 9.0 / 5.0 + 32.0) } pub fn to_kelvin(self) -> Kelvin { Kelvin(self.0 + 273.15) }}impl Fahrenheit { pub fn to_celsius(self) -> Celsius { Celsius((self.0 - 32.0) * 5.0 / 9.0) } pub fn to_kelvin(self) -> Kelvin { self.to_celsius().to_kelvin() }}impl Kelvin { pub fn to_celsius(self) -> Celsius { Celsius(self.0 - 273.15) } pub fn to_fahrenheit(self) -> Fahrenheit { self.to_celsius().to_fahrenheit() }}#[derive(Debug, Copy, Clone, PartialEq)]pub enum Temperature { Celsius(Celsius), Fahrenheit(Fahrenheit), Kelvin(Kelvin),}impl Temperature { pub fn to_celsius(self) -> Celsius { match self { Temperature::Celsius(c) => c, Temperature::Fahrenheit(f) => f.to_celsius(), Temperature::Kelvin(k) => k.to_celsius(), } } pub fn to_fahrenheit(self) -> Fahrenheit { match self { Temperature::Celsius(c) => c.to_fahrenheit(), Temperature::Fahrenheit(f) => f, Temperature::Kelvin(k) => k.to_fahrenheit(), } } pub fn to_kelvin(self) -> Kelvin { match self { Temperature::Celsius(c) => c.to_kelvin(), Temperature::Fahrenheit(f) => f.to_kelvin(), Temperature::Kelvin(k) => k, } } pub fn try_convert_from_str(self, string: &str) -> Result<Self, String> { match string { CELSIUS_CHAR_STR => Ok(Temperature::Celsius(self.to_celsius())), FAHRENHEIT_CHAR_STR => Ok(Temperature::Fahrenheit(self.to_fahrenheit())), KELVIN_CHAR_STR => Ok(Temperature::Kelvin(self.to_kelvin())), _ => Err(INVALID_INPUT_ERROR_STR.into()), } }}impl TryFrom<(&str, f64)> for Temperature { type Error = String; fn try_from(tuple: (&str, f64)) -> Result<Self, Self::Error> { match tuple { (CELSIUS_CHAR_STR, num) => Ok(Temperature::Celsius(Celsius(num))), (FAHRENHEIT_CHAR_STR, num) => Ok(Temperature::Fahrenheit(Fahrenheit(num))), (KELVIN_CHAR_STR, num) => Ok(Temperature::Kelvin(Kelvin(num))), _ => Err(INVALID_INPUT_ERROR_STR.into()), } }}impl From<Temperature> for f64 { fn from(temperature: Temperature) -> Self { match temperature { Temperature::Celsius(Celsius(v)) => v, Temperature::Fahrenheit(Fahrenheit(v)) => v, Temperature::Kelvin(Kelvin(v)) => v, } }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let initial_unit_temperature: Temperature = (from_unit, value).try_into()?; let converted_unit_temperature = initial_unit_temperature.try_convert_from_str(to_unit)?; Ok(converted_unit_temperature.into())}
const UNITS: [&str; 3] = ["K", "C", "F"];pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if !UNITS.contains(&from_unit) || !UNITS.contains(&to_unit) { return Err("Invalid unit".to_string()); } let result: f64 = match (from_unit, to_unit) { ("K", "C") => value - 273.15, ("C", "K") => value + 273.15, ("K", "F") => (value - 273.15) * (9.0/5.0) + 32.0, ("F", "K") => (value - 32.0) * (5.0/9.0) + 273.15, ("C", "F") => value * (9.0/5.0) + 32.0, ("F", "C") => (value - 32.0) * (5.0/9.0), (_, _) => -1000.0, }; return Ok(result);}
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. / 5.) + 32.), ("C", "K") => Ok(value + 273.15), ("F", "K") => Ok((value - 32.) * (5. / 9.) + 273.15), ("F", "C") => Ok((value - 32.) * (5. / 9.)), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * (9. / 5.) + 32.), _ => Err("Invalid unit".to_string()) }}
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./5.) + 32.), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * (9./5.) + 32.), ("F", "C") => Ok(((value - 32.) * (5./9.))), ("F", "K") => Ok(((value - 32.) * (5./9.)) + 273.15), _ => Err("Invalid unit".to_string()) }}
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) { ("K", "K") => Ok(value), ("C", "C") => Ok(value), ("F", "F") => Ok(value), ("K", "C") => Ok(value-273.15), ("K", "F") => Ok((value-273.15)*(9./5.) + 32.), ("C", "K") => Ok(value + 273.15), ("C", "F") => Ok(value*(9./5.) + 32.), ("F", "K") => Ok((value-32.)*(5./9.) + 273.15), ("F", "C") => Ok((value-32.)*(5./9.)), _ => Err("Invalid unit".to_string()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if from_unit == to_unit {return Ok(value)} match from_unit { "C" => match to_unit { "F" => Ok(convert_cf(value)), "K" => Ok(convert_ck(value)), _ => Err("Invalid unit".to_string()), }, "F" => match to_unit { "C" => Ok(convert_fc(value)), "K" => Ok(convert_ck(convert_fc(value))), _ => Err("Invalid unit".to_string()), }, "K" => match to_unit { "C" => Ok(convert_kc(value)), "F" => Ok(convert_cf(convert_kc(value))), _ => Err("Invalid unit".to_string()), }, _ => Err("Invalid unit".to_string()), }}fn convert_cf(input: f64) -> f64 { input * (9.0 / 5.0) + 32.0}fn convert_ck(input: f64) -> f64 { input + 273.15}fn convert_kc(input: f64) -> f64 { input - 273.15}fn convert_fc(input: f64) -> f64 { (input - 32.0) * (5.0 / 9.0)}
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(convert_cf(value)), "K" => Ok(convert_ck(value)), _ => Err("Invalid unit".to_string()), }, "F" => match to_unit { "C" => Ok(convert_fc(value)), "F" => Ok(value), "K" => Ok(convert_ck(convert_fc(value))), _ => Err("Invalid unit".to_string()), }, "K" => match to_unit { "C" => Ok(convert_kc(value)), "F" => Ok(convert_cf(convert_kc(value))), "K" => Ok(value), _ => Err("Invalid unit".to_string()), }, _ => Err("Invalid unit".to_string()), }}fn convert_cf(input: f64) -> f64 { input * (9.0 / 5.0) + 32.0}fn convert_ck(input: f64) -> f64 { input + 273.15}fn convert_kc(input: f64) -> f64 { input - 273.15}fn convert_fc(input: f64) -> f64 { (input - 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(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 to_celcius = convert_temperature(value, "F", "C")?; convert_temperature(to_celcius, "C", "K") }, ("K", "F") => { let to_celcius = convert_temperature(value, "K", "C")?; convert_temperature(to_celcius, "C", "F") }, _ => Err("Invalid unit".to_string()), }}
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)), ("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("Invalid unit".to_string()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if from_unit == to_unit { return Ok(value); } if from_unit == "C"{ if to_unit == "F"{ return Ok((value * 9.0 / 5.0) + 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.0 / 9.0); } if to_unit == "K"{ return Ok((value - 32.0) * 5.0 / 9.0 + 273.15); } } if from_unit == "K"{ if to_unit == "C"{ return Ok(value - 273.15); } if to_unit == "F"{ return Ok((value - 273.15) * 9.0 / 5.0 + 32.0); } } return Err("Invalid unit".to_string());}
fn c_to_f(val: f64) -> f64 { val * (9_f64/5_f64) + 32_f64}fn f_to_c(val: f64) -> f64 { (val - 32_f64) * (5_f64/9_f64)}fn c_to_k(val: f64) -> f64 { val + 273.15_f64}fn k_to_c(val: f64) -> f64 { val - 273.15_f64}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { return match from_unit { "C" => match to_unit { "F" => Ok(c_to_f(value)), "K" => Ok(c_to_k(value)), _ => Err("Invalid unit".to_string()), }, "F" => match to_unit { "C" => Ok(f_to_c(value)), "K" => Ok(c_to_k(f_to_c(value))), _ => Err("Invalid unit".to_string()), }, "K" => match to_unit { "C" => Ok(k_to_c(value)), "F" => Ok(c_to_f(k_to_c(value))), _ => Err("Invalid unit".to_string()), } _ => Err("Invalid unit".to_string()), }}