In many real-world applications, converting temperatures between different units is a common task. For example, meteorologists, scientists, and engineers often need to convert temperatures from Celsius to Fahrenheit or Kelvin. In this challenge, you'll implement a function that converts temperatures between these units using logical operators and conditional statements.
Your task is to write a function convert_temperature
that takes three parameters: a float representing the temperature value, a string representing the unit of the input temperature, and another string representing the desired unit for the output temperature. The function should return a Result
type with either the converted temperature as a float or an error message if the input is invalid.
"Invalid unit"
.Ok
variant of the Result
.The Celsius and Fahrenheit scales are named after the scientists Anders Celsius and Daniel Gabriel Fahrenheit, respectively. The Kelvin scale, on the other hand, is named after Lord Kelvin, a British physicist. Unlike the Celsius and Fahrenheit scales, which are based on the freezing and boiling points of water, the Kelvin scale is an absolute temperature scale based on the concept of absolute zero, the lowest possible temperature where all molecular motion ceases.
F = C * (9/5) + 32
C = (F - 32) * (5/9)
K = C + 273.15
C = K - 273.15
sander-b-postnl
#[derive(PartialEq)]enum Unit { Celcius, Fahrenheit, Kelvin}impl Unit { fn parse(unit: &str) -> Result<Self, String> { match unit { "C" => Ok(Self::Celcius), "F" => Ok(Self::Fahrenheit), "K" => Ok(Self::Kelvin), _ => Err("Invalid unit".to_string()), } } fn convert(self, to: Unit, value: f64) -> Result<f64, String> { if to == self { return Ok(value); } match (self, to) { (Self::Celcius, Self::Fahrenheit) => Ok(value * (9.0 / 5.0) + 32.0), (Self::Fahrenheit, Self::Celcius) => Ok((value - 32.0) * (5.0 / 9.0)), (Self::Celcius, Self::Kelvin) => Ok(value + 273.15), (Self::Kelvin, Self::Celcius) => Ok(value - 273.15), (Self::Fahrenheit, Self::Kelvin) => Ok(((value - 32.0) * (5.0 / 9.0)) + 273.15), (Self::Kelvin, Self::Fahrenheit) => Ok((value - 273.15) * (9.0 / 5.0) + 32.0), (_, _) => Err("Unable to convert".to_string()) } }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let from_unit = Unit::parse(from_unit)?; let to_unit = Unit::parse(to_unit)?; from_unit.convert(to_unit, value)}
Bhardwaj08
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(String::from("Invalid unit")) }}
savasozturk
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { (from, to) if from == to => Ok(value), ("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".into()), }}
zavakid
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) { (from, to) if from == to => Ok(value), ("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".into()), }}
mk-comm
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if !["C", "K", "F"].contains(&from_unit) { return Err("Invalid unit".to_string()); } if !["C", "K", "F"].contains(&to_unit) { return Err("Invalid unit".to_string()); } if from_unit == "C" && to_unit == "F" { Ok(value * (9.0 / 5.0) + 32.0) } else if from_unit == "C" && to_unit == "K" { Ok(value + 273.15) } else if from_unit == "F" && to_unit == "C" { Ok((value - 32.0) * (5.0 / 9.0)) } else if from_unit == "F" && to_unit == "K" { Ok((value - 32.0) * (5.0 / 9.0) + 273.15) } else if from_unit == "K" && to_unit == "C" { Ok(value - 273.15) } else if from_unit == "K" && to_unit == "F" { Ok((value - 273.15) * (9.0 / 5.0) + 32.0) } else if from_unit == to_unit { Ok(value) // No conversion needed } else { Err("Invalid conversion".to_string()) }}
shmaxuti
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit.to_owned() + to_unit).as_str() { "CF" => Ok(value * (9.0 / 5.0) + 32.0), "CK" => Ok(value + 273.15), "FC" => Ok((value - 32.0) * 5.0 / 9.0), "FK" => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), "KC" => Ok(value - 273.15), "KF" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()), }}
carlos-quantexai
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); // Corrected formula } 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 if from_unit == "K" && to_unit == "F" { return Ok((value - 273.15) * 9.0 / 5.0 + 32.0); } else { return Err("Invalid unit".to_string()) }}
carlos-quantexai
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); // Corrected formula } 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 if from_unit == "K" && to_unit == "F" { return Ok((value - 273.15) * 9.0 / 5.0 + 32.0); } else { return Err("Invalid unit".to_string()) }}
jose-bernardo
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) { ("F", "C") => { return Ok((value - 32.0) / 1.8 ); }, ("C", "F") => { return Ok(value * 1.8 + 32.0); }, ("F", "K") => { return Ok((value - 32.0) / 1.8 + 273.15); }, ("K", "F") => { return Ok((value - 273.15) * 1.8 + 32.0); }, ("C", "K") => { return Ok(value + 273.15); }, ("K", "C") => { return Ok(value - 273.15); }, _ => { return Err("Invalid unit".to_string()); } }}
Mxn-ptr
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) { (b, c) if b == "C" && c == "F" => Ok(value * 9.0 / 5.0 + 32.0), (b, c) if b == "F" && c == "C" => Ok((value - 32.0) * 5.0 / 9.0), (b, c) if b == "C" && c == "K" => Ok(value + 273.15), (b, c) if b == "K" && c == "C" => Ok(value - 273.15), (b, c) if b == "F" && c == "K" => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), (b, c) if b == "K" && c == "F" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()) }}
Mxn-ptr
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) { (b, c) if b == "C" && c == "F" => Ok(value * 9.0 / 5.0 + 32.0), (b, c) if b == "F" && c == "C" => Ok((value - 32.0) * 5.0 / 9.0), (b, c) if b == "C" && c == "K" => Ok(value + 273.15), (b, c) if b == "K" && c == "C" => Ok(value - 273.15), (b, c) if b == "F" && c == "K" => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), (b, c) if b == "K" && c == "F" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()) }}
Mxn-ptr
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) { (b, c) if b == "C" && c == "F" => Ok(value * 9.0 / 5.0 + 32.0), (b, c) if b == "F" && c == "C" => Ok((value - 32.0) * 5.0 / 9.0), (b, c) if b == "C" && c == "K" => Ok(value + 273.15), (b, c) if b == "K" && c == "C" => Ok(value - 273.15), (b, c) if b == "F" && c == "K" => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), (b, c) if b == "K" && c == "F" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()) }}
yansq
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") => convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K"), ("K", "F") => convert_temperature(convert_temperature(value, "K", "C").unwrap(), "C", "F"), ("C", "C") | ("F", "F") | ("K", "K") => Ok(value), _ => Err("Invalid unit".to_string()) }}
hinphansa
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit.to_owned() + to_unit).as_str() { "CF" => Ok(value * 9.0 / 5.0 + 32.0), "CK" => Ok(value + 273.15), "FC" => Ok((value - 32.0) * 5.0 / 9.0), "FK" => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), "KC" => Ok(value - 273.15), "KF" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()), }}
hinphansa
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("Invalid unit".to_string()), }, "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("Invalid unit".to_string()), }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()), }, _ => Err("Invalid unit".to_string()), }}
funny233-github
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let kelvin; match from_unit { "C" => { kelvin = value + 273.15; } "F" => { kelvin = (value - 32.0) * 5.0 / 9.0 + 273.15; } "K" => { kelvin = value; } _ => return Err(format!("Invalid unit")), } match to_unit { "C" => { return Ok(kelvin - 273.15); } "F" => { return Ok((kelvin - 273.15) * 9.0 / 5.0 + 32.0); } "K" => { return Ok(kelvin); } _ => return Err(format!("Invalid unit")), }}
digitalresistor
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)), ("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()) }}
majesticalcreature
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here Ok(Temp{ unit: Unit::conv(from_unit)?, temp: value }.conv_to(Unit::conv(to_unit)?) .temp)}#[derive(Copy, Clone)]enum Unit { Kelvin, Celsius, Fahrenheit}impl Unit { fn conv(unit: &str) -> Result<Unit, String> { match unit { "C" => Ok(Unit::Celsius), "F" => Ok(Unit::Fahrenheit), "K" => Ok(Unit::Kelvin), _ => Err(String::from("Invalid unit")) } }}struct Temp { unit: Unit, temp: f64}impl Temp { fn conv_to(&self, unit: Unit) -> Temp { self.to_celsius().from_celsius(unit) } fn to_celsius(&self) -> Temp { Temp { unit: Unit::Celsius, temp: match self.unit { Unit::Celsius => self.temp, Unit::Kelvin => self.temp - 273.15, Unit::Fahrenheit => (self.temp - 32.0) * 5.0 / 9.0 }} } fn from_celsius(&self, unit: Unit) -> Temp { Temp { unit: unit, temp: match unit { Unit::Celsius => self.temp, Unit::Kelvin => self.temp + 273.15, Unit::Fahrenheit => (self.temp * 9.0 / 5.0) + 32.0 }} }}
Aditeya
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C", "F") => Ok(value * (9.0/5.0) + 32.0), ("F", "C") => Ok((value-32.0) * (9.0/5.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => { let c = convert_temperature(value, "K", "C")?; convert_temperature(c, "C", "F") } ("F", "K") => { let c = convert_temperature(value, "F", "C")?; convert_temperature(c, "C", "K") } _ => Err("Invalid unit".into()) }}
jhq223
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { fn c_f(value: f64) -> f64 { value * (9.0 / 5.0) + 32.0 } fn f_c(value: f64) -> f64 { (value - 32.0) * (5.0 / 9.0) } fn c_k(value: f64) -> f64 { value + 273.15 } fn k_c(value: f64) -> f64 { value - 273.15 } match (from_unit, to_unit) { ("C", "F") => Ok(c_f(value)), ("F", "C") => Ok(f_c(value)), ("C", "K") => Ok(c_k(value)), ("K", "C") => Ok(k_c(value)), ("F", "K") => Ok(c_k(f_c(value))), ("K", "F") => Ok(c_f(k_c(value))), _ => { Err("Invalid unit".to_string()) } }}
xbarnett
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let kelvin: f64 = match from_unit { "C" => value + 273.15, "F" => (value - 32.0) * (5.0/9.0) + 273.15, "K" => value, _ => return Err(String::from("Invalid unit")), }; match to_unit { "K" => Ok(kelvin), "C" => Ok(kelvin - 273.15), "F" => Ok((kelvin - 273.15) * (9.0/5.0) + 32.0), _ => Err(String::from("Invalid unit")), }}
matei
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match from_unit { "K" => match to_unit { "C" => Ok(value - 273.15), "F" => convert_temperature(convert_temperature(value, "K", "C").unwrap(), "C", "F"), _ => Err(String::from("Invalid unit")) }, "F" => match to_unit { "C" => Ok((value - 32.0) * (5/9) as f64), "K" => convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K"), _ => Err(String::from("Invalid unit")) }, "C" => match to_unit { "F" => Ok(value * (9.0/5.0) + 32.0), "K" => Ok(value + 273.15), _ => Err(String::from("Invalid unit")) }, _ => Err(String::from("Invalid unit")) }}
oneopane
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) { ("F", "C") => Ok((value - 32.) * 5. / 9.), ("F", "K") => Ok((value - 32.) * 5. / 9. + 273.15), ("C", "K") => Ok(value + 273.15), ("C", "F") => Ok(value * (9./5.) + 32.), ("K", "F") => Ok((value - 273.15) * 9. / 5. + 32.), ("K", "C") => Ok(value - 273.15), _ => Err("Invalid unit".to_string()) }}
oneopane
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) { ("F", "C") => Ok((value - 32.) * 5. / 9.), ("F", "K") => Ok((value - 32.) * 5. / 9. + 273.15), ("C", "K") => Ok(value + 273.15), ("C", "F") => Ok(value * (9./5.) + 32.), ("K", "F") => Ok((value - 273.15) * 9. / 5. + 32.), ("K", "C") => Ok(value - 273.15), _ => Err("Invalid unit".to_string()) }}
amassare
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit,to_unit){ ("C","F")=>Ok(c2f(value)), ("F","C")=>Ok(f2c(value)), ("C","K")=>Ok(value+273.15), ("F","K")=>Ok(f2c(value)+273.15), ("K","C")=>Ok(value-273.15), ("K","F")=>Ok(c2f(value-273.15)), ("F","F")|("C","C")|("K","K")=>Ok(value), _=>Err("Invalid unit".to_string()), }}fn f2c(value:f64)->f64{ (value-32.0)*(5.0/9.0)}fn c2f(value:f64)->f64{ value*(9.0/5.0)+32.0}
CianciuStyles
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) }; match from_unit { "C" => match to_unit { "F" => Ok(c_to_f(value)), "K" => Ok(value + 273.15), _ => Err(String::from("Invalid unit")) }, "F" => { let c = f_to_c(value); match to_unit { "C" => Ok(c), "K" => Ok(c + 273.15), _ => Err(String::from("Invalid unit")) } }, "K" => { let c = value - 273.15; match to_unit { "C" => Ok(c), "F" => Ok(c_to_f(c)), _ => Err(String::from("Invalid unit")) } }, _ => Err(String::from("Invalid unit")) }}
pbjarterot
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let res = 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()) }; res}
morigs
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), ("C", "C") => Ok(value), ("F", "F") => Ok(value), ("F", "K") => Ok((value - 32.0) * (5.0/9.0) + 273.15), ("F", "C") => Ok((value - 32.0) * (5.0/9.0)), ("K", "F") => Ok((value - 273.15) * (9.0/5.0) + 32.0), ("K", "K") => Ok(value), ("K", "C") => Ok(value - 273.15), _ => Err("Invalid unit".to_string()), }}
kyhou
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let from_value_kelvin: f64; match from_unit { "C" => from_value_kelvin = value + 273.15, "F" => from_value_kelvin = (value - 32.0) * 5.0/9.0 + 273.15, "K" => from_value_kelvin = value, _ => { return Err("Invalid unit".to_string()); }, } match to_unit { "C" => Ok(from_value_kelvin - 273.15), "F" => Ok((from_value_kelvin - 273.15) * 9.0/5.0 + 32.0), "K" => Ok(from_value_kelvin), _ => Err("Invalid unit".to_string()), }}
ayushrawat10
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match from_unit { "C" => { match to_unit { "K" => { return Ok(value+273.15); }, "F" => { return Ok(value*(9.0/5.0 as f64) + 32.0); }, _ => { return Err("Invalid unit".to_string()); } } }, "K" => { match to_unit { "C" => { return Ok(value-273.15); }, "F" => { return Ok(convert_temperature(value-273.15, "C", "F")?); }, _ => { return Err("Invalid unit".to_string()); } } }, "F" => { match to_unit { "C" => { return Ok((value-32.0)*(5.0/9.0)) }, "K" => { return Ok(convert_temperature(convert_temperature(value, "F", "C")?, "C", "K")?); }, _ => { return Err("Invalid unit".to_string()); } } }, _ => { return Err("Invalid unit".to_string()); } }}
masteryachty
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" { Ok(value * (9f64 / 5f64) + 32f64) } else if from_unit == "F" && to_unit == "C" { Ok((value - 32f64) * (5f64 / 9f64)) } else if from_unit == "C" && to_unit == "K" { Ok(value + 273.15) } else if from_unit == "K" && to_unit == "C" { Ok(value - 273.15) } else if from_unit == "F" && to_unit == "K" { Ok((value - 32f64) * (5f64 / 9f64) + 273.15) } else if from_unit == "K" && to_unit == "F" { Ok((value - 273.15) * (9f64 / 5f64) + 32f64) } else { Err(String::from("Invalid unit")) }}
damascussteel21
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" { Ok(value * (9f64 / 5f64) + 32f64) } else if from_unit == "F" && to_unit == "C" { Ok((value - 32f64) * (5f64 / 9f64)) } else if from_unit == "C" && to_unit == "K" { Ok(value + 273.15) } else if from_unit == "K" && to_unit == "C" { Ok(value - 273.15) } else if from_unit == "F" && to_unit == "K" { Ok((value - 32f64) * (5f64 / 9f64) + 273.15) } else if from_unit == "K" && to_unit == "F" { Ok((value - 273.15) * (9f64 / 5f64) + 32f64) } else { Err(String::from("Invalid unit")) }}
damascussteel21
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" { Ok(value * (9f64 / 5f64) + 32f64) } else if from_unit == "F" && to_unit == "C" { Ok((value - 32f64) * (5f64 / 9f64)) } else if from_unit == "C" && to_unit == "K" { Ok(value + 273.15) } else if from_unit == "K" && to_unit == "C" { Ok(value - 273.15) } else if from_unit == "F" && to_unit == "K" { Ok((value - 32f64) * (5f64 / 9f64) + 273.15) } else if from_unit == "K" && to_unit == "F" { Ok((value - 273.15) * (9f64 / 5f64) + 32f64) } else { Err(String::from("Invalid unit")) }}
edgarcnp
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit){ ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("F", "K") => Ok(((value - 32.0) * (5.0 / 9.0)) + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * (9.0 / 5.0) + 32.0), _ => Err("Invalid unit".to_string()) }}
wischi-chr
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if from_unit == to_unit { return Ok(value); } let celcius = match from_unit { "C" => value, "F" => (value - 32.0) * 5.0 / 9.0, "K" => value - 273.15, _ => return Err("Invalid unit".to_string()), }; Ok(match to_unit { "C" => celcius, "F" => celcius * 9.0 / 5.0 + 32.0, "K" => celcius + 273.15, _ => return Err("Invalid unit".to_string()), })}
jtruong04
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let valid_units = vec!["C","F","K"]; if !valid_units.contains(&from_unit) || !valid_units.contains(&to_unit) { return Err("Invalid unit".to_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()) }}
jaswgreen
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 * (9f64/5f64) + 32f64), "K" => Ok(value + 273.15f64), _ => Err("Invalid unit".to_string()), }, "F"=> match to_unit { "C" => Ok((value - 32f64) * (5f64/9f64)), "K" => Ok((value - 32f64) * (5f64/9f64) + 273.15f64), _ => Err("Invalid unit".to_string()), }, "K"=> match to_unit { "C" => Ok(value - 273.15f64), "F" => Ok((value - 273.15f64) * (9f64/5f64) + 32f64), _ => Err("Invalid unit".to_string()), }, _ => Err("Invalid unit".to_string()) }}
CarrotTarrot
const COEFFICIENT: f64 = 9.0 / 5.0;pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "C") => Ok(value), ("F", "F") => Ok(value), ("C", "F") => Ok(value * COEFFICIENT + 32.0), ("F", "C") => Ok((value - 32.0) / COEFFICIENT), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) / COEFFICIENT + 273.15), ("K", "F") => Ok((value - 273.15) * COEFFICIENT + 32.0), (_, _) => Err("Invalid unit".to_string()) }}
Sommos
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here const COEFFICIENT: f64 = 9.0 / 5.0; match (from_unit, to_unit) { ("C", "C") => Ok(value), ("F", "F") => Ok(value), ("C", "F") => Ok(value * COEFFICIENT + 32.0), ("F", "C") => Ok((value - 32.0) / COEFFICIENT), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) / COEFFICIENT + 273.15), ("K", "F") => Ok((value - 273.15) * COEFFICIENT + 32.0), (_, _) => Err("Invalid unit".to_string()) }}
CarrotTarrot
const COEFFICIENT: f64 = 9.0 / 5.0;pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "C") => Ok(value), ("F", "F") => Ok(value), ("C", "F") => Ok(value * COEFFICIENT + 32.0), ("F", "C") => Ok((value - 32.0) / COEFFICIENT), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) / COEFFICIENT + 273.15), ("K", "F") => Ok((value - 273.15) * COEFFICIENT + 32.0), (_, _) => Err("Invalid unit".to_string()) }}
CarrotTarrot
const COEFFICIENT: f64 = 9.0 / 5.0;pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "C") => Ok(value), ("F", "F") => Ok(value), ("C", "F") => Ok(value * COEFFICIENT + 32.0), ("F", "C") => Ok((value - 32.0) / COEFFICIENT), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) / COEFFICIENT + 273.15), ("K", "F") => Ok((value - 273.15) * COEFFICIENT + 32.0), (_, _) => Err("Invalid unit".to_string()) }}
tukantje
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), "K" => Ok(value + 273.15), "F" => Ok(value * (9f64/5f64) + 32f64), _ => Err("Invalid unit".to_string()) }, "K" => match to_unit { "C" => Ok(value - 273.15), "K" => Ok(value), "F" => Ok((value - 273.15) * (9f64/5f64) + 32f64), _ => Err("Invalid unit".to_string()) }, "F" => match to_unit { "C" => Ok((value - 32f64) * (5f64/9f64)), "K" => Ok((value - 32f64) * (5f64/9f64) + 273.15), "F" => Ok(value), _ => Err("Invalid unit".to_string()) }, _ => Err("Invalid unit".to_string()) }}
imxiaohui
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * 1.8 + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value-32.0)/1.8), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value-32.0)/1.8 + 273.15), ("K", "F") => Ok((value - 273.15) * 1.8 + 32.0), _ => Err("Invalid unit".to_string()), }}
dyoo
use std::str::FromStr;enum Measurement { C, F, K,}impl FromStr for Measurement { type Err = String; fn from_str(s: &str) -> Result<Measurement, String> { match s { "C" => Ok(Measurement::C), "K" => Ok(Measurement::K), "F" => Ok(Measurement::F), _ => Err("Invalid unit".to_string()), } }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let from : Measurement = from_unit.parse()?; let to: Measurement = to_unit.parse()?; use Measurement::{C, F, K}; Ok(match (from, to) { (C, C) | (F, F) | (K, K) => value, (C, F) => value * 9./5. + 32., (C, K) => value + 273.15, (F, C) => (value - 32.) * 5./9., (K, C) => value - 273.15, (F, K) => convert_temperature( convert_temperature(value, "F", "C")?, "C", "K")?, (K, F) => convert_temperature( convert_temperature( value, "K", "C")?, "C", "F")? }) // TODO: Implement the function here}
dyoo
use std::str::FromStr;enum Measurement { C, F, K,}impl FromStr for Measurement { type Err = String; fn from_str(s: &str) -> Result<Measurement, String> { match s { "C" => Ok(Measurement::C), "K" => Ok(Measurement::K), "F" => Ok(Measurement::F), _ => Err("Invalid unit".to_string()), } }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let from : Measurement = from_unit.parse()?; let to: Measurement = to_unit.parse()?; use Measurement::{C, F, K}; Ok(match (from, to) { (C, C) | (F, F) | (K, K) => value, (C, F) => value * 9./5. + 32., (C, K) => value + 273.15, (F, C) => (value - 32.) * 5./9., (K, C) => value - 273.15, (F, K) => convert_temperature( convert_temperature(value, "F", "C")?, "C", "K")?, (K, F) => convert_temperature( convert_temperature( value, "K", "C")?, "C", "F")? }) // TODO: Implement the function here}
hilias
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); } let celsius_to_fahrenheit = |c| c * 9.0 / 5.0 + 32.0; let fahrenheit_to_celsius = |f| (f - 32.0) * 5.0/9.0; let celsius_to_kelvin = |c| c + 273.15; let kelvin_to_celsius = |k| k - 273.15; if from_unit == "F" { if to_unit == "K" { Ok(celsius_to_kelvin(fahrenheit_to_celsius(value))) } else { Ok(fahrenheit_to_celsius(value)) } } else if from_unit == "C" { if to_unit == "K" { Ok(celsius_to_kelvin(value)) } else { Ok(celsius_to_fahrenheit(value)) } } else { if to_unit == "F" { Ok(celsius_to_fahrenheit(kelvin_to_celsius(value))) } else { Ok(kelvin_to_celsius(value)) } }}
qcabanes-hobby
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * (9f64 / 5f64) + 32f64), ("C", "K") => Ok(value + 273.15f64), ("F", "C") => Ok((value - 32f64) * (5f64 / 9f64)), ("F", "K") => Ok((value - 32f64) * (5f64 / 9f64) + 273.15f64), ("K", "C") => Ok(value - 273.15f64), ("K", "F") => Ok((value - 273.15f64) * (9f64 / 5f64) + 32f64), (_, _) => Err("Invalid unit".to_string()), }}
Apollo-Roboto
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(String::from("Invalid unit")) }}
TaiPoole
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * 9.0 / 5.0 + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) * 5.0 / 9.0), ("F", "K") => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), _ => Err("Invalid unit".to_string()) }}
TomBaston
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 { "C" => Ok(value), "F" => Ok(value * 1.8 + 32.0), "K" => Ok(value + 273.15), _ => Err("Invalid unit".into()) }, "F" => match to_unit { "C" => Ok((value - 32.0) * (5.0/9.0)), "F" => Ok(value), "K" => Ok((value - 32.0) * (5.0/9.0) + 273.15), _ => Err("Invalid unit".into()) }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => Ok((value - 273.15) * 1.8 + 32.0), "K" => Ok(value), _ => Err("Invalid unit".into()) }, _ => Err("Invalid unit".into()) }}