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) + 32C = (F - 32) * (5/9)K = C + 273.15C = K - 273.15pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let c_to_f = |value: f64| { value * (9.0 / 5.0) + 32.0 }; let c_to_k = |value: f64| { value + 273.15 }; let f_to_c = |value: f64| { (value - 32.0) * (5.0 / 9.0) }; let k_to_c = |value: f64| { value - 273.15 }; match (from_unit, to_unit) { ("C", "C") | ("F", "F") | ("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(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", "F") => Ok((value * (9f64 / 5f64)) + 32f64), ("F", "C") => Ok((value - 32f64) * (5f64 / 9f64)), ("C", "K") => Ok((value + 273f64) + 0.15), ("K", "C") => Ok((value - 273f64) - 0.15), ("F", "K") => Ok((convert_temperature(value, "F", "C"))? + 273f64 + 0.15), ("K", "F") => Ok(((value - 273f64 - 0.15) * (9f64 / 5f64)+ 32f64).round()), (_, _) => 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) { ("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), ("K", "F") => Ok((value - 273.15) * (9.0/5.0) + 32.0), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 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) { ("C", "F") => { Ok((value * (9.0/5.0)) + 32 as f64) }, ("C", "K") => { Ok(value + 273.15) }, ("F", "C") => { Ok((value - 32 as f64) * (5.0/9.0)) }, ("K","C") => { Ok(value - 273.15) }, ("F","K") => { Ok((value - 32 as f64 ) * (5.0/9.0) + 273.15) }, ("K", "F") => { Ok((value - 273.15) * (9.0/5.0) + 32 as f64) }, _ => 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), ("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()), }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let result = 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, ("F", "K") => (value - 32.0) * (5.0 / 9.0) + 273.15, ("K", "F") => (value - 273.15) * (9.0 / 5.0) + 32.0, _ => return Err("Invalid unit".to_string()), }; Ok(result)}use std::str::FromStr;enum ConversionUnit { Celsius(f64), Kelvin(f64), Fahrenheit(f64),}impl ConversionUnit { fn _convert_celsius_to(&self, target: &str) -> Result<ConversionUnit, String> { let value = self.get_value(); // Extract the value match target { "F" => Ok(ConversionUnit::Fahrenheit(value * (9.0/5.0) + 32.0)), "K" => Ok(ConversionUnit::Kelvin(value + 273.15)), _ => Err("Invalid unit".to_string()) } } fn _convert_fahrenheit_to(&self, target: &str) -> Result<ConversionUnit, String> { let value = self.get_value(); match target { "C" => Ok(ConversionUnit::Celsius((value - 32.0) * (5.0/9.0))), "K" => Ok(ConversionUnit::Kelvin((value - 32.0) * (5.0/9.0) + 273.15)), _ => Err("Invalid unit".to_string()) } } fn _convert_kelvin_to(&self, target: &str) -> Result<ConversionUnit, String> { let value = self.get_value(); match target { "C" => Ok(ConversionUnit::Celsius(value - 273.15)), "F" => Ok(ConversionUnit::Fahrenheit((value - 273.15) * (9.0/5.0) + 32.0)), _ => Err("Invalid unit".to_string()) } } fn convert_to(&self, target: &str) -> Result<ConversionUnit, String> { match self { Self::Celsius(_) => self._convert_celsius_to(target), Self::Fahrenheit(_) => self._convert_fahrenheit_to(target), Self::Kelvin(_) => self._convert_kelvin_to(target) } } // Helper method to extract the value fn get_value(&self) -> f64 { match self { ConversionUnit::Celsius(val) => *val, ConversionUnit::Kelvin(val) => *val, ConversionUnit::Fahrenheit(val) => *val, } } fn from_str(s: &str, val: f64) -> Option<Self> { match s { "C" => Some(ConversionUnit::Celsius(val)), "K" => Some(ConversionUnit::Kelvin(val)), "F" => Some(ConversionUnit::Fahrenheit(val)), _ => None, } } }fn is_conversion_available(unit : &str) -> bool { ConversionUnit::from_str(unit, 1f64).is_some()}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match ConversionUnit::from_str(from_unit, value).unwrap().convert_to(to_unit) { Ok(res) => Ok(res.get_value()), Err(text) => Err(text) } }pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let to_cel = match from_unit{ "C" => Ok(value), "F" => Ok(f_to_c(value)), "K" => Ok(k_to_c(value)), _ => Err("Invalid unit".to_string()) }; let result = match to_unit{ "C" => Ok(to_cel?), "F" => Ok(c_to_f(to_cel?)), "K" => Ok(c_to_k(to_cel?)), _ => Err("Invalid unit".to_string()) }; return Ok(result?);}fn c_to_f(c:f64) -> f64{ (c * 9.0/5.0) + 32.0 }fn f_to_c(f:f64) -> f64{ (f - 32.0) * 5.0/9.0}fn c_to_k(c : f64) -> f64{ c + 273.15}fn k_to_c(k : f64) -> f64{ k - 273.15}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","K") => {Ok((value - 32.0)*(5.0/9.0)+273.15)}, ("F","C") => {Ok((value - 32.0) * (5.0/9.0))}, ("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> { // 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","K") => {Ok((value - 32.0)*(5.0/9.0)+273.15)}, ("F","C") => {Ok((value - 32.0) * (5.0/9.0))}, ("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> { // 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") => convert_temperature((value - 32.0) * (5.0/9.0), "C", "K"), ("K", "C") => Ok(value - 273.15), ("K", "F") => convert_temperature(value - 273.15, "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" { if to_unit == "F" { Ok(value * (9.0/5.0) + 32.0) } else if to_unit == "K" { Ok(value + 273.15) } else { Err("Invalid unit".to_string()) } } else if from_unit == "F" { if to_unit == "C" { Ok((value - 32.0) * (5.0/9.0)) } else if to_unit == "K" { convert_temperature((value - 32.0) * (5.0/9.0), "C", "K") } else { Err("Invalid unit".to_string()) } } else if from_unit == "K" { if to_unit == "C" { Ok(value - 273.15) } else if to_unit == "F" { convert_temperature(value - 273.15, "C", "F") } else { Err("Invalid unit".to_string()) } } else { 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), ("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(format!("Invalid unit")), }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let valid = ["C", "F", "K"]; if valid.contains(&from_unit) && valid.contains(&to_unit) { if from_unit == to_unit { return Ok(value); } else if from_unit == "C" && to_unit == "F" { return Ok(value * 1.8 + 32.0); } else if from_unit == "C" && to_unit == "K" { return Ok(value + 273.15); } else if from_unit == "F" && to_unit == "C" { return Ok((value - 32.0) * 5.0 / 9.0); } else if from_unit == "F" && to_unit == "K" { return Ok((value - 32.0) * 5.0 / 9.0 + 273.15); } else if from_unit == "K" && to_unit == "C" { return Ok(value - 273.15); } else { return Ok((value - 273.15) * 1.8 + 32.0); } } else { 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", "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), ("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(String::from("Invalid unit")), }}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), ("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(String::from("Invalid unit")), }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if !matches!(from_unit, "C" | "K" | "F") { return Err("Invalid unit".to_string()); } if !matches!(to_unit, "C" | "K" | "F") { return Err("Invalid unit".to_string()); } if from_unit == "C" && to_unit == "K" { return Ok(value + 273.15); } if from_unit == "K" && to_unit == "C" { return Ok(value - 273.15); } if from_unit == "C" && to_unit == "F" { return Ok(value * (9.0/5.0) + 32.0); } if from_unit == "F" && to_unit == "C" { return Ok((value - 32.0) * (5.0/9.0)); } if from_unit == "K" && to_unit == "F" { return Ok((value - 273.15) * (9.0/5.0) + 32.0); } if from_unit == "F" && to_unit == "K" { return Ok((value - 32.0) * (5.0/9.0) + 273.15); } return Ok(value);}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let celsius = match from_unit { "C" => Ok(value), "F" => Ok((value - 32.0) * 5.0 / 9.0), "K" => Ok(value - 273.15), _ => Err("Invalid unit".to_string()), }?; let result = match to_unit { "C" => Ok(celsius), "F" => Ok(celsius * 9.0 / 5.0 + 32.0), "K" => Ok(celsius + 273.15), _ => Err("Invalid unit".to_string()), }?; Ok(result)}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let celsius = 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" => celsius, "F" => celsius * 9.0 / 5.0 + 32.0, "K" => celsius + 273.15, _ => return Err("Invalid unit".to_string()), }; Ok(result)}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let j = [from_unit, to_unit].join(""); match j.as_str() { "CF" => { return Ok(value * (9.0/5.0) + 32.0); }, "FC" => { return Ok((value - 32.0) * (5.0/9.0)); }, "FK" => { return Ok((value - 32.0) * (5.0/9.0) + 273.15); }, "KF" => { return Ok((value - 273.15) * (5.0/9.0) + 32.0); }, "CK" => { return Ok(value + 273.15); }, "KC" => { return Ok(value - 273.15); }, _ => { return 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,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","F")=>Ok((value - 273.15) * (9.0/5.0) + 32.0), ("K","C")=>Ok(value - 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 valid_units = ["C", "F", "K"]; let from_unit = from_unit.to_uppercase(); let to_unit = to_unit.to_uppercase(); if !valid_units.contains(&from_unit.as_str()) || !valid_units.contains(&&to_unit.as_str()) { return Err("Invalid unit".to_string()); } let celsius = match from_unit.as_str() { "C" => value, "F" => (value - 32.0) * (5.0/9.9), "K" => value - 273.15, _ => unreachable!() }; let result = match to_unit.as_str() { "C" => celsius, "F" => (celsius * (9.0/5.0)) + 32.0, "K" => celsius + 273.15, _ => 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), ("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()) }}use std::str::FromStr;enum Temperature { Celcius, Fahrenheit, Kelvin,}impl FromStr for Temperature { type Err = String; fn from_str(s: &str) -> Result<Self, Self::Err> { match s { "C" => Ok(Temperature::Celcius), "F" => Ok(Temperature::Fahrenheit), "K" => Ok(Temperature::Kelvin), _ => 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 use Temperature as Temp; match (Temp::from_str(from_unit)?, Temp::from_str(to_unit)?) { (Temp::Celcius, Temp::Fahrenheit) => Ok(value * (9.0/5.0) + 32.0), (Temp::Fahrenheit, Temp::Celcius) => Ok((value - 32.0) * (5.0/9.0)), (Temp::Celcius, Temp::Kelvin) => Ok(value + 273.15), (Temp::Kelvin, Temp::Celcius) => Ok(value - 273.15), (Temp::Fahrenheit, Temp::Kelvin) => Ok((value - 32.0) * (5.0/9.0) + 273.15), (Temp::Kelvin, Temp::Fahrenheit) => Ok((value - 273.15) * (9.0/5.0) + 32.0), _ => Ok(value) }}pub fn c_to_f(value: f64) -> f64 { value * (9.0/5.0) + 32.0}pub fn f_to_c(value: f64) -> f64 { (value - 32.0) * (5.0/9.0)}pub fn c_to_k(value: f64) -> f64 { value + 273.15}pub fn k_to_c(value: f64) -> f64 { value - 273.15}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here if from_unit == to_unit { return Ok(1.0); } let var = (from_unit, to_unit); match var { ("C", "F") => Ok( c_to_f(value) ), ("F", "C") => Ok( f_to_c(value) ), ("C", "K") => Ok( c_to_k(value) ), ("K", "C") => Ok( k_to_c(value) ), ("F", "K") => Ok( c_to_k(f_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 let valid_units: [&str;3]=["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) } let mut result = match (from_unit, to_unit) { ("C", "F") => value * 9.0 / 5.0 + 32.0, ("C", "K") => value + 273.15, ("F", "C") => (value - 32.0) * 5.0 / 9.0, ("F", "K") => (value - 32.0) * 5.0 / 9.0 + 273.15, ("K", "C") => value - 273.15, ("K", "F") => (value - 273.15) * 9.0 / 5.0 + 32.0, _ => value, // misma unidad }; // Redondear a 2 decimales result = (result * 100.0).round() / 100.0; 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), ("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> { // 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), ("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> { // 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()) }}