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> { 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()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here // // Start by converting any incoming value to SI unit, "K". // let mut temp_value = value; // match from_unit { // "C" => temp_value = value + 273.15, // "F" => temp_value = (value - 32.0)*(5./9.)+ 273.15, // "K" => (), // _ => return Err("Invalid unit".to_string()) // } // // Convert from K to to_unit. // match to_unit { // "C" => Ok(temp_value - 273.15), // "F" => Ok((temp_value - 273.15)*(9./5.) + 32.), // "K" => Ok(temp_value), // _ => Err("Invalid unit".to_string()), // } // This implementation can do the whole thing in one match statement, but it requires a known list of all possible pairs. This seems like it would become cumbersome if there were many from/to pairs. In this case there are 3 possible units (K, C, F) so 3^2 arms in the match statement. If we add Rankine then there are 4^2 arms. What if this were a length converter, there are so many length options! Seems like it would be easier to read and maintain using the two match statements above, becuase each one is has n arms, not n^2. 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> { // TODO: Implement the function here // // Start by converting any incoming value to SI unit, "K". // let mut temp_value = value; // match from_unit { // "C" => temp_value = value + 273.15, // "F" => temp_value = (value - 32.0)*(5./9.)+ 273.15, // "K" => (), // _ => return Err("Invalid unit".to_string()) // } // // Convert from K to to_unit. // match to_unit { // "C" => Ok(temp_value - 273.15), // "F" => Ok((temp_value - 273.15)*(9./5.) + 32.), // "K" => Ok(temp_value), // _ => Err("Invalid unit".to_string()), // } // This implementation can do the whole thing in one match statement, but it requires a know list of all possible pairs. 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> { 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" => 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 as f64), "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 here match from_unit { "C" => match to_unit { "F" => Ok(value * 9.0 / 5.0 + 32.0), "K" => Ok(value + 273.15), _ => Err(format!("Invalid unit")), }, "F" => match to_unit { "C" => Ok((value - 32.0) * 5.0 / 9.0), "K" => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), _ => Err(format!("Invalid unit")), }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err(format!("Invalid unit")), }, _ => Err(format!("Invalid unit")), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here if from_unit == "K" && to_unit == "C" { return Ok(value-273.15); }; if from_unit == "C" && to_unit == "K" { return Ok(value+273.15); }; if from_unit == "F" && to_unit == "C" { return Ok((value-32.0)*5.0/9.0); }; if from_unit == "C" && to_unit == "F" { return Ok((value*9.0/5.0) + 32.0); }; if from_unit == "F" && to_unit == "K" { return Ok((value-32.0)*5.0/9.0 + 273.15); }; if from_unit == "K" && to_unit == "F" { return Ok((value - 273.15)*9.0/5.0 + 32.0); }; return 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 == "K" && to_unit == "C" { return Ok(value-273.15); }; if from_unit == "C" && to_unit == "K" { return Ok(value+273.15); }; if from_unit == "F" && to_unit == "C" { return Ok((value-32.0)*5.0/9.0); }; if from_unit == "C" && to_unit == "F" { return Ok((value*9.0/5.0) + 32.0); }; if from_unit == "F" && to_unit == "K" { return Ok((value-32.0)*5.0/9.0 + 273.15); }; if from_unit == "K" && to_unit == "F" { return Ok((value - 273.15)*9.0/5.0 + 32.0); }; return 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 { "C" => Ok((value - 32.0) * (5.0 / 9.0)), "F" => Ok(value), "K" => convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K"), _ => Err("Invalid unit".to_string()), }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => convert_temperature(convert_temperature(value, "K", "C").unwrap(), "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> { 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) / 1.8 + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * 1.8 + 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, to_unit) { ("C", "F") => Ok(value * (9f64 / 5f64) + 32f64), ("F", "C") => Ok((value - 32f64) * (5f64 / 9f64)), ("C", "K") => Ok(value + 273.15f64), ("K", "C") => Ok(value - 273.15f64), ("F", "K") => Ok((value - 32f64) * 5f64 / 9f64 + 273.15f64), ("K", "F") => Ok((value - 273.15f64) * 9f64 / 5f64 + 32f64), (_, _) => 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.), ("F", "C") => Ok((value - 32.) * (5./9.)), ("F", "K") => Ok((value - 32.) * (5./9.)+ 273.15), ("C", "K") => Ok(value + 273.15), ("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> { match (from_unit, to_unit) { ("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.), ("C", "C") | ("F", "F") | ("K", "K") => Ok(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 * 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) / 1.8 + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * 1.8 + 32.0), _ => Err(String::from("Invalid unit")) }}
const VALID_UNITS: [&str; 3] = ["C", "F", "K"];pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here if !VALID_UNITS.contains(&from_unit) || !VALID_UNITS.contains(&to_unit) { return Err("Invalid unit".to_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()), }}
const VALID_UNITS: [&str; 3] = ["C", "F", "K"];pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here if !VALID_UNITS.contains(&from_unit) || !VALID_UNITS.contains(&to_unit) { return Err("Invalid unit".to_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 match (from_unit, to_unit) { ("C", "F") => Ok(value * (9./5.) + 32.), ("F", "C") => Ok((value - 32.) * (5./9.)), ("K", "C") => Ok(value - 273.15), ("C", "K") => Ok(value + 273.15), ("K", "F") => Ok(convert_temperature(convert_temperature(value, "K", "C").unwrap(), "C", "F").unwrap()), ("F", "K") => Ok(convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K").unwrap()), (_, _) => 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.), ("F", "C") => Ok((value - 32.) * (5./9.)), ("K", "C") => Ok(value - 273.15), ("C", "K") => Ok(value + 273.15), ("K", "F") => Ok(convert_temperature(value - 273.15, "C", "F").unwrap()), ("F", "K") => Ok(convert_temperature(value, "F", "C").unwrap() + 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", "F") => Ok((value - 273.15) * 1.8 + 32.0), ("K", "C") => 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), ("C", "F") => Ok(value * (9.0/5.0) + 32.0), ("C", "K") => Ok(value + 273.15), (_, _) => 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 if from_unit == "C" && to_unit == "F" { return Ok(value * (9.0/5.0) + 32.0); } else if from_unit == "F" && to_unit == "C" { let res = (value - 32.0) * (5.0/9.0); return Ok(res); } else if from_unit == "K" && to_unit == "F" { return Ok((value - 273.15) * (9.0/5.0) + 32.0); } else if from_unit == "F" && to_unit == "K" { let res = ((value - 32.0) * (5.0/9.0)) + 273.15; return Ok(res); } else if from_unit == "C" && to_unit == "K" { return Ok(value + 273.15); } else if from_unit == "K" && to_unit == "C" { return Ok(value - 273.15); } return 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 if !["C", "F", "K"].contains(&from_unit) || !["C", "F", "K"].contains(&to_unit) { return Err("Invalid unit".to_string()); } if from_unit == to_unit { return Ok(value); } let c_f_mul: f64 = 9.0/5.0; if from_unit == "C" && to_unit == "F" { return Ok(value * c_f_mul + 32.0); } if from_unit == "F" && to_unit == "C" { return Ok((value - 32.0) * c_f_mul); } let c_k_flat : f64=273.15; if from_unit == "C" && to_unit == "K" { return Ok(value + c_k_flat); } if from_unit == "K" && to_unit == "C" { return Ok(value - c_k_flat); } if from_unit == "F" && to_unit == "K" { return Ok((value - 32.0) * c_f_mul + c_k_flat); } if from_unit == "K" && to_unit == "F" { return Ok((value - c_k_flat) * c_f_mul + 32.0 ); } Err("Invalid unit".to_string())}
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 converted = match to_unit { "C" => celsius, "F" => celsius * (9.0 / 5.0) + 32.0, "K" => celsius + 273.15, _ => unreachable!(), }; Ok(converted)}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "C") => 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", "F") => Ok(value), ("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), ("K", "K") => Ok(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), ("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) * 1.8 + 32.0), ("F", "K") => Ok((value + 459.67) * 5.0 / 9.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", "K") => Ok(value + 273.15), ("C", "F") => Ok(value * (9.0/5.0) + 32.0), ("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), (a, b) if a == b => Ok(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 * 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".into()), }}
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(cel_to_far(value)), ("C", "K") => Ok(cel_to_kelv(value)), ("F", "C") => Ok(far_to_cels(value)), ("F", "K") => Ok(far_to_kelv(value)), ("K", "C") => Ok(kelv_to_cels(value)), ("K", "F") => Ok(kelv_to_far(value)), _ => Err("Invalid unit".to_string()), }}fn cel_to_far(value: f64) -> f64 { value * (9. / 5.) + 32.}fn cel_to_kelv(value: f64) -> f64 { value + 273.15}fn kelv_to_cels(value: f64) -> f64 { value - 273.15}fn kelv_to_far(value: f64) -> f64 { cel_to_far(kelv_to_cels(value))}fn far_to_cels(value: f64) -> f64 { (value - 32.) * (5. / 9.)}fn far_to_kelv(value: f64) -> f64 { cel_to_kelv(far_to_cels(value))}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C","F") => Ok(cel_to_far(value)), ("F","C") => Ok(far_to_cel(value)), ("C","K") => Ok(cel_to_kel(value)), ("K","C") => Ok(kel_to_cel(value)), ("K","F") => Ok(kel_to_far(value)), ("F","K") => Ok(far_to_kel(value)), _ => Err("Invalid unit".to_string()) }}fn cel_to_far(value: f64) -> f64 { value * (9.0/5.0) + 32.0}fn far_to_cel(value: f64) -> f64 { (value - 32.0) * (5.0/9.0)}fn cel_to_kel(value: f64) -> f64 { value + 273.15}fn kel_to_cel(value: f64) -> f64 { value - 273.15}fn kel_to_far(value: f64) -> f64 { cel_to_far(kel_to_cel(value))}fn far_to_kel(value: f64) -> f64 { cel_to_kel(far_to_cel(value))}
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(cel_to_far(value)), ("F","C") => Ok(far_to_cel(value)), ("C","K") => Ok(cel_to_kel(value)), ("K","C") => Ok(kel_to_cel(value)), ("K","F") => Ok(kel_to_far(value)), ("F","K") => Ok(far_to_kel(value)), _ => Err("Invalid unit".to_string()) }}fn cel_to_far(value: f64) -> f64 { value * (9.0/5.0) + 32.0}fn far_to_cel(value: f64) -> f64 { (value - 32.0) * (5.0/9.0)}fn cel_to_kel(value: f64) -> f64 { value + 273.15}fn kel_to_cel(value: f64) -> f64 { value - 273.15}fn kel_to_far(value: f64) -> f64 { cel_to_far(kel_to_cel(value))}fn far_to_kel(value: f64) -> f64 { cel_to_kel(far_to_cel(value))}
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(cel_to_far(value)), ("F","C") => Ok(far_to_cel(value)), ("C","K") => Ok(cel_to_kel(value)), ("K","C") => Ok(kel_to_cel(value)), ("K","F") => Ok(kel_to_far(value)), ("F","K") => Ok(far_to_kel(value)), _ => Err("Invalid unit".to_string()) }}fn cel_to_far(value: f64) -> f64 { value * (9.0/5.0) + 32.0}fn far_to_cel(value: f64) -> f64 { (value - 32.0) * (5.0/9.0)}fn cel_to_kel(value: f64) -> f64 { value + 273.15}fn kel_to_cel(value: f64) -> f64 { value - 273.15}fn kel_to_far(value: f64) -> f64 { cel_to_far(kel_to_cel(value))}fn far_to_kel(value: f64) -> f64 { cel_to_kel(far_to_cel(value))}
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(from_c_to_f(value)), ("C","K") => Ok(from_c_to_k(value)), ("F","C") => Ok(from_f_to_c(value)), ("F","K") => Ok(from_f_to_k(value)), ("K","C") => Ok(from_k_to_c(value)), ("K","F") => Ok(from_k_to_f(value)), _ => Err(String::from("Invalid unit")) }}const ZEROABSOLUTE: f64 = 273.15; fn from_c_to_f(value: f64)->f64{ (value * (9.0/5.0)) + 32.0 }fn from_c_to_k(value:f64)-> f64{ value + ZEROABSOLUTE }fn from_f_to_c(value:f64)->f64{ ((value-32.0)*5.0)/9.0}fn from_f_to_k(value: f64) -> f64 { from_c_to_k(from_f_to_c(value)) }fn from_k_to_c(value:f64)->f64{ value - ZEROABSOLUTE}fn from_k_to_f(value:f64)->f64{ from_c_to_f(from_k_to_c(value)) }
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match from_unit{ "C" => from_c_to_other(value, to_unit), "F" => from_f_to_other(value, to_unit), "K" => from_k_to_other(value, to_unit), _ => Err(String::from("Invalid unit")) }}fn from_c_to_other(value:f64, to_unit:&str)->Result<f64, String>{ match to_unit { "C" => Ok(value), "F" => Ok(from_c_to_f(value)), "K" => Ok(from_c_to_k(value)), _ => Err(String::from("Invalid unit")) }}fn from_f_to_other(value:f64, to_unit:&str)->Result<f64, String>{ match to_unit { "F" => Ok(value), "C" => Ok(from_f_to_c(value)), "K" => Ok(from_f_to_k(value)), _ => Err(String::from("Invalid unit")) }}fn from_k_to_other(value:f64, to_unit:&str)->Result<f64, String>{ match to_unit { "K" => Ok(value), "F" => Ok(from_k_to_f(value)), "C" => Ok(from_k_to_c(value)), _ => Err(String::from("Invalid unit")) }}fn from_c_to_f(value: f64)->f64{ (value * (9.0/5.0)) + 32.0 }fn from_c_to_k(value:f64)-> f64{ value + 273.15 }fn from_f_to_c(value:f64)->f64{ ((value-32.0)*5.0)/9.0}fn from_f_to_k(value:f64)->f64{ from_f_to_c(value) + 273.15 }fn from_k_to_c(value:f64)->f64{ value - 273.15}fn from_k_to_f(value:f64)->f64{ let c = from_k_to_c(value); from_c_to_f(c) }
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let input : f64 = match from_unit { "F" => (value - 32.) * 5./9. + 273.15, "C" => value + 273.15, "K" => value, _ => -1., }; if input < 0. { Err("Invalid unit".to_string()) } else { match to_unit { "F" => Ok((input - 273.15) * 9./5. + 32.), "C" => Ok(input - 273.15), "K" => Ok(input), _ => 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((value - 32.) * (5./9.) + 273.15), ("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 { "C" => match to_unit { "F" => Ok(value * (9./5.) + 32.), "K" => Ok(value + 273.15), _ => Err("Invalid unit".to_string()) }, "F" => match to_unit { "C" => Ok((value - 32.) * (5. / 9.)), "K" => convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K"), _ => Err("Invalid unit".to_string()) }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => convert_temperature(convert_temperature(value, "K", "C").unwrap(), "C", "F"), _ => 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 here match from_unit { "C" => match to_unit { "K" => Ok(value + 273.15), "F" => Ok(value * (9.0 / 5.0) + 32.0), _ => Err("Invalid unit".to_string()), }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => { let c = convert_temperature(value, "K", "C").unwrap(); convert_temperature(c, "C", "F") } _ => Err("Invalid unit".to_string()), }, "F" => match to_unit { "C" => Ok((value - 32.0) * (5.0 / 9.0)), "K" => { let c = convert_temperature(value, "F", "C").unwrap(); convert_temperature(c, "C", "K") } _ => 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 here match from_unit { "C" => match to_unit { "K" => Ok(value + 273.15), "F" => Ok(value * (9.0 / 5.0) + 32.0), _ => Err("Invalid unit".to_string()), }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => { let c = convert_temperature(value, "K", "C").unwrap(); convert_temperature(c, "C", "F") } _ => Err("Invalid unit".to_string()), }, "F" => match to_unit { "C" => Ok((value - 32.0) * (5.0 / 9.0)), "K" => { let c = convert_temperature(value, "F", "C").unwrap(); convert_temperature(c, "C", "K") } _ => Err("Invalid unit".to_string()), }, _ => Err("Invalid unit".to_string()), }}