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> { 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()), }}
fn cel_to_fah(value: f64) -> f64 { //F = C * (9/5) + 32 return value * (9.0/5.0) + 32.0}fn fah_to_cel(value: f64) -> f64 { //C = (F - 32) * (5/9) return (value - 32.0) * (5.0 / 9.0)}fn cel_to_kel(value: f64) -> f64 { //K = C + 273.15 return value + 273.15}fn kel_to_cel(value: f64) -> f64 { //C = K - 273.15 return value - 273.15}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => return Ok(cel_to_fah(value)), ("F", "C") => return Ok(fah_to_cel(value)), ("C", "K") => return Ok(cel_to_kel(value)), ("K", "C") => return Ok(kel_to_cel(value)), ("F", "K") => return Ok(cel_to_kel(fah_to_cel(value))), ("K", "F") => return Ok(cel_to_fah(kel_to_cel(value))), _ => return Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let in_k = 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")), }; return match to_unit { "C" => Ok(in_k - 273.15), "F" => Ok((in_k - 273.15) * 9.0 / 5.0 + 32.0), "K" => Ok(in_k), _ => 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), ("F","C") => Ok((value - 32.0) * (5.0 / 9.0)), ("C","K") => Ok(value + 273.15), ("K","C") => Ok(value - 273.15), ("F","K") => Ok(convert_temperature(value,from_unit,"C").unwrap() + 273.15), ("K","F") => Ok(convert_temperature(value- 273.15,"C",to_unit).unwrap()), (_,_) => 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), ("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", "F") => Ok(value * (9.0/5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * (9.0/5.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok(((value - 32.0) * (9.0/5.0)) + 273.15), ("K", "F") => Ok((value - 273.15) * (9.0/5.0) + 32.0), _ => Err(String::from("Invalid unit")) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { println!("convert_template {}, {}, {}", value, from_unit, to_unit); match from_unit { "C" => { match to_unit { "F" => { Ok((value * (9.0 / 5.0)) + 32 as f64) }, "K" => { Ok(value + 273.15) }, _ => Err("Invalid unit".to_string()) } }, "F" => { match to_unit { "C" => { Ok((value - 32 as f64) * (5.0 / 9.0)) }, "K" => { Ok(((value - 32 as f64) * (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 as f64) }, _ => 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), ("F", "C") => Ok((value - 32.0) * (5.0/9.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok(((value - 32.0) * (5.0/9.0)) + 273.15), ("K", "F") => Ok((value - 273.15) * (9.0/5.0) + 32.0), _ => Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) * (5.0 / 9.0) + 273.15), ("K", "F") => Ok((value - 273.15) * (9.0 / 5.0) + 32.0), _ => Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * 9.0 / 5.0 + 32.0), ("F", "C") => Ok((value - 32.0) * 5.0 / 9.0), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), ("K", "F") => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), ("C", "C") | ("F", "F") | ("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), ("F", "K") => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), ("K", "F") => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), ("C", "C") | ("F", "F") | ("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 { "C" | "F" | "K" => (), _ => return Err("Invalid unit".to_string()) } match to_unit { "C" | "F" | "K" => (), _ => 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", "F") => Ok(value), ("F", "C") => Ok((value - 32.0) * (5.0/9.0)), ("F", "K") => Ok((value - 32.0) / (9.0/5.0) + 273.15), ("K", "K") => Ok(value), ("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), ("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 celsius = (value - 32.0) * 5.0 / 9.0; Ok(celsius + 273.15) } ("K", "F") => { let celsius = value - 273.15; Ok(celsius * 9.0 / 5.0 + 32.0) } ("C", "C") | ("F", "F") | ("K", "K") => Ok(value), // Handle same unit conversion _ => 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" { if to_unit=="F" { return Ok(c_to_f(value)); } if to_unit=="K" { return Ok(c_to_k(value)); } } if from_unit=="K" { let c = k_to_c(value); if to_unit=="C" { return Ok(c); } if to_unit=="F" { return Ok(c_to_f(c)); } } if from_unit=="F" { let c = f_to_c(value); if to_unit=="C" { return Ok(c); } if to_unit=="K" { return Ok(c_to_k(c)); } } return Err("Invalid unit".to_string());}fn c_to_f(value:f64)->f64 { return value * (9.0/5.0) + 32.0;}fn f_to_c(value:f64)->f64 { return (value-32.0) * (5.0/9.0);}fn c_to_k(value:f64)->f64 { return value + 273.15;}fn k_to_c(value:f64)->f64 { return value - 273.15;}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit){ ("C", "F") => Ok(value * 1.8 + 32.0), ("C", "K") => Ok(value + 273.15), ("F", "C") => Ok((value - 32.0) * (5.0/9.0)), ("F", "K") => Ok(((value - 32.0) * (5.0/9.0)) + 273.15), ("K", "F") => Ok((value - 273.15) * 1.8 + 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 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") => convert_temperature(value, "F", "C").unwrap() + 273.15, ("K", "C") => value - 273.15, ("K", "F") => convert_temperature(value, "K", "C").unwrap() * (9.0/5.0) + 32.0, _ => -111111.0 }; if result == -111111.0 { Err("Invalid unit".to_string()) } else { 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", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("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()), }}
fn ctof(c: f64) -> f64 { c * (9.0 / 5.0) + 32.0 }fn ftoc(f: f64) -> f64 { (f - 32.0) * (5.0/9.0) }fn ctok(c: f64) -> f64 { c + 273.15 }fn ktoc(k: f64) -> f64 { k - 273.15 }fn ftok(f: f64) -> f64 { ctok(ftoc(f)) }fn ktof(k: f64) -> f64 { ctof(ktoc(k)) }pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (value, from_unit, to_unit) { (c, "C", "F") => Ok(ctof(c)), (f, "F", "C") => Ok(ftoc(f)), (c, "C", "K") => Ok(ctok(c)), (k, "K", "C") => Ok(ktoc(k)), (c, "F", "K") => Ok(ftok(c)), (k, "K", "F") => Ok(ktof(k)), _ => Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let src_celsius: Celsius = match from_unit { "C" => Celsius { value }, "F" => Celsius::from(Fahrenheit { value }), "K" => Celsius::from(Kelvin { value }), _ => Err("Invalid unit".to_string())? }; let res: f64 = match to_unit { "C" => src_celsius.value, "F" => Fahrenheit::from(src_celsius).value, "K" => Kelvin::from(src_celsius).value, _ => Err("Invalid unit".to_string())? }; Ok(res)}struct Celsius {value: f64}struct Fahrenheit {value: f64}struct Kelvin {value: f64}impl From<Fahrenheit> for Celsius { fn from(f: Fahrenheit) -> Self { Celsius {value: (f.value - 32.0) * (5.0/9.0)} }}impl From<Kelvin> for Celsius { fn from(f: Kelvin) -> Self { Celsius {value: (f.value - 273.15)} }}impl From<Celsius> for Fahrenheit { fn from(f: Celsius) -> Self { Fahrenheit {value: f.value * (9.0/5.0) + 32.0} }}impl From<Celsius> for Kelvin { fn from(f: Celsius) -> Self { Kelvin {value: (f.value + 273.15)} }}
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_owned()) }, "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_owned()) }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), "K" => Ok(value), _ => Err("Invalid unit".to_owned()) }, _ => Err("Invalid unit".to_owned()) }}
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_owned()) }, "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_owned()) }, "K" => match to_unit { "C" => Ok(value - 273.15), "F" => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), "K" => Ok(value), _ => Err("Invalid unit".to_owned()) }, _ => Err("Invalid unit".to_owned()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * (9.0 / 5.0)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * (9. / 5.) + 32.0), ("F", "K") => Ok((value - 32.) * (9. / 5.) + 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. / 5.) + 32.), ("F", "C") => Ok((value - 32.) * (9. / 5.)), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * (9. / 5.) + 32.), ("F", "K") => Ok((value - 32.) * (9. / 5.) + 273.15), _ => Err("Invalid unit".to_string()) }}
use std::ops::Add;const KDELTA: f64 = 273.15;const FDELTA: f64 = 32.0;const FFACTOR: f64 = 1.8;fn celsius2kelvin(temp_celsius: f64) -> f64 { temp_celsius + KDELTA}fn kelvin2celsius(temp_kelvin: f64) -> f64 { temp_kelvin - KDELTA}fn fahrenheit2celsius(temp_faherenheit: f64) -> f64 { (temp_faherenheit - FDELTA) / FFACTOR}fn celsius2fahrenheit(temp_celsius: f64) -> f64 { temp_celsius * FFACTOR + FDELTA}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match String::from(from_unit).add(to_unit).as_str() { "CK" => Ok(celsius2kelvin(value)), "CF" => Ok(celsius2fahrenheit(value)), "KC" => Ok(kelvin2celsius(value)), "KF" => Ok(celsius2fahrenheit(kelvin2celsius(value))), "FC" => Ok(fahrenheit2celsius(value)), "FK" => Ok(celsius2kelvin(fahrenheit2celsius(value))), _ => Err("Invalid unit".to_string()), }}
fn celtokel(t:f64) -> f64{ t+273.15}fn fahrtokel(t:f64) -> f64{ celtokel((t - 32.00) / 1.8)}fn keltocel(t:f64) -> f64{ t-273.15}fn keltofahr(t:f64) -> f64{ keltocel(t) * 1.8 + 32.0 }pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { let e = Err("Invalid unit".to_string()); // TODO: Implement the function here let kelvin_value = match from_unit{ "C"=>celtokel(value), "F"=>fahrtokel(value), "K"=>value, _=>-1.0 }; if kelvin_value == -1.0 { return e; } match to_unit{ "C"=>Ok(keltocel(kelvin_value)), "F"=>Ok(keltofahr(kelvin_value)), "K"=>Ok(kelvin_value), _=>e }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") => Ok(value * (9.0/5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * 5.0/9.0), ("C", "K") => Ok(value + 273.15), ("K", "C") => Ok(value - 273.15), ("F", "K") => Ok(convert_temperature(convert_temperature(value, "F", "C").unwrap(), "C", "K").unwrap()), ("K", "F") => Ok(convert_temperature(convert_temperature(value, "K", "C").unwrap(), "C", "F").unwrap()), _ => Err("Invalid unit".to_string()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { match (from_unit, to_unit) { ("C", "F") =>{ Ok(value * (9f64 / 5f64) + 32f64) }, ("F", "C") =>{ Ok((value - 32f64) * (5f64 / 9f64)) }, ("C", "K") =>{ Ok(value + 273.15) }, ("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()) } }}
use std::ops::Add;const CEL_TO_FAR: f64 = (9f64 / 5f64);const FAR_TO_CEL: f64 = (5f64 / 9f64);pub struct Fahrenheit(f64);pub struct Celsius(f64);pub struct Kelvin(f64);impl From<Celsius> for Fahrenheit{ fn from(value: Celsius) -> Self { Self(value.0 * (CEL_TO_FAR) + 32f64) }}impl From< Fahrenheit> for Celsius{ fn from(value: Fahrenheit) -> Self { Self((value.0 - 32f64)* (FAR_TO_CEL) ) }}impl From<Celsius> for Kelvin{ fn from(value: Celsius) -> Self { Self(value.0 + 273.15f64) }}impl From<Kelvin> for Celsius{ fn from(value: Kelvin) -> Self { Self(value.0 - 273.15f64) }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let s:String = from_unit.to_string().add(to_unit); match s.as_str(){ "FC" => Ok(Celsius::from(Fahrenheit(value)).0), "CF" => Ok(Fahrenheit::from(Celsius(value)).0), "KC" => Ok(Celsius::from(Kelvin(value)).0), "CK" => Ok(Kelvin::from(Celsius(value)).0), "FK" => Ok(Kelvin::from(Celsius::from(Fahrenheit(value))).0), "KF" => Ok(Fahrenheit::from(Celsius::from(Kelvin(value))).0), _ => Err("Invalid unit".to_string()), } }
use std::ops::Add;const CEL_TO_FAR: f64 = (9f64 / 5f64);const FAR_TO_CEL: f64 = (5f64 / 9f64);pub struct Fahrenheit(f64);pub struct Celsius(f64);pub struct Kelvin(f64);impl From<Celsius> for Fahrenheit{ fn from(value: Celsius) -> Self { Self(value.0 * (CEL_TO_FAR) + 32f64) }}impl From< Fahrenheit> for Celsius{ fn from(value: Fahrenheit) -> Self { Self((value.0 - 32f64)* (FAR_TO_CEL) ) }}impl From<Celsius> for Kelvin{ fn from(value: Celsius) -> Self { Self(value.0 + 273.15f64) }}impl From<Kelvin> for Celsius{ fn from(value: Kelvin) -> Self { Self(value.0 - 273.15f64) }}pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here let s:String = from_unit.to_string().add(to_unit); match s.as_str(){ "FC" => Ok(Celsius::from(Fahrenheit(value)).0), "CF" => Ok(Fahrenheit::from(Celsius(value)).0), "KC" => Ok(Celsius::from(Kelvin(value)).0), "CK" => Ok(Kelvin::from(Celsius(value)).0), "FK" => Ok(Kelvin::from(Celsius::from(Fahrenheit(value))).0), "KF" => Ok(Fahrenheit::from(Celsius::from(Kelvin(value))).0), _ => {Err("Invalid unit".to_string())}, } }
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { fn validate_unit(unit: &str) -> bool { matches!(unit, "C" | "F" | "K") } if !validate_unit(from_unit) || !validate_unit(to_unit) { return Err(String::from("Invalid unit")); } match (from_unit, to_unit) { ("C", "F") => Ok(value * 1.8 + 32.0), ("F", "C") => Ok((value - 32.0) / 1.8), ("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")), } }
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // Pre-calculate conversion factors (minor performance gain, mainly stylistic) const C_TO_F_MULTIPLIER: f64 = 9.0 / 5.0; const F_TO_C_MULTIPLIER: f64 = 5.0 / 9.0; let celsius = match from_unit { "C" => value, "K" => value - 273.15, "F" => (value - 32.0) * F_TO_C_MULTIPLIER, _ => return Err("Invalid unit".to_string()), }; match to_unit { "C" => Ok(celsius), "K" => Ok(celsius + 273.15), "F" => Ok(celsius * C_TO_F_MULTIPLIER + 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 let input_c = match from_unit { "C" => value, "K" => value - 273.15, "F" => (value - 32.0) * ((5.0/9.0) as f64), _ => return Err("Invalid unit".to_string()), }; match to_unit { "C" => return Ok(input_c), "K" => return Ok(input_c + 273.15), "F" => return Ok(input_c * ((9.0/5.0) as f64) + 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, to_unit) { ("F", "C") => Ok((value - 32.0) / 1.8), ("C", "F") => Ok(value * 1.8 + 32.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"), _ => 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), ("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 fromTo = (from_unit,to_unit); match fromTo { ("C", "F") => return Ok(value * (9.0/5.0) + 32.0), ("F", "C") => return Ok((value - 32.0) * (5.0/9.0)), ("C", "K") => return Ok(value + 273.15), ("K", "C") => return Ok(value - 273.15), ("F", "K") => return Ok((value - 32.0) * (5.0/9.0) + 273.15), ("K", "F") => return 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", "C") | ("F", "F") | ("K", "K") => Ok(value), ("C", "F") => Ok(value * (9.0 / 5.0) + 32.0), ("C", "K") => Ok(273.15 - value), ("K", "C") => Ok(value - 273.15), ("K", "F") => Ok((value - 273.15) * (9.0 / 5.0) + 32.0), ("F", "C") => Ok((value - 32.0) * (5.0 / 9.0)), ("F", "K") => Ok(273.15 - (value - 32.0) * (5.0 / 9.0)), _ => Err("Invalid unit".into()), }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { if from_unit == "C" && to_unit == "F" { Ok(value * 9.0 / 5.0 + 32.0) } else if from_unit == "F" && to_unit == "C" { Ok((value - 32.0) * 5.0 / 9.0) } 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 - 32.0) * 5.0 / 9.0 + 273.15) } else if from_unit == "K" && to_unit == "F" { Ok((value - 273.15) * 9.0 / 5.0 + 32.0) } else { Err("Invalid unit".to_string()) }}
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { // TODO: Implement the function here match (from_unit, to_unit) { ("C","F") => Ok(value * (9_f64/5_f64) + 32.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") => match convert_temperature(value, "F", "C") { Ok(n) => convert_temperature(n, "C", "K"), Err(e) => Err(e), }, ("K","F") => match convert_temperature(value, "K", "C") { Ok(n) => convert_temperature(n, "C", "F"), Err(e) => Err(e), }, _ => 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("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()) }}
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") => return Ok(value * (9.0/5.0) + 32.0), ("F", "C") => return Ok((value - 32.0) * (5.0/9.0)), ("C", "K") => return Ok(value + 273.15), ("K", "C") => return Ok(value - 273.15), ("F", "K") => return Ok((value - 32.0) * (5.0/9.0) + 273.15), ("K", "F") => return 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> { 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")=>match convert_temperature(value, "F", "C") { Ok(n)=>convert_temperature(n, "C", "K"), Err(e)=>Err(e) }, ("K", "C")=>Ok(value-273.15), ("K", "F")=>match convert_temperature(value, "K", "C") { Ok(n)=>convert_temperature(n, "C", "F"), Err(e)=>Err(e) }, _=>Err("Invalid unit".to_string()) }}