Remo Senekowitsch 360344ab6c
Simplify story and increase difficulty
Conversion between Celsius and Fahrenheit should be understandable to
most. Inverting the formula is still not very hard, but a little harder
than only multiplying by 100.
2026-05-15 10:41:35 +02:00

55 lines
1.4 KiB
Rust

// The `From` trait is used for value-to-value conversions. If `From` is
// implemented, an implementation of `Into` is automatically provided.
// You can read more about it in the documentation:
// https://doc.rust-lang.org/std/convert/trait.From.html
//
// Representing units of measurements with separate types is a common practice.
// It avoids accidentally mixing up values of different units of measurement.
struct Celsius(f64);
struct Fahrenheit(f64);
impl From<Celsius> for Fahrenheit {
// TODO: Convert Celsius to Fahrenheit. Don't worry about floating-point
// precision. The formula is: F = C * 1.8 + 32
}
impl From<Fahrenheit> for Celsius {
// TODO: Convert Fahrenheit to Celsius.
}
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
const CASES: [(f64, f64); 6] = [
(-50.0, -58.0),
(0.0, 32.0),
(20.0, 68.0),
(100.0, 212.0),
(400.0, 752.0),
(1000.0, 1832.0),
];
#[test]
fn celsius_to_fahrenheit() {
for (celsius, fahrenheit) in CASES {
let Fahrenheit(actual) = Celsius(celsius).into();
assert_eq!(actual.round(), fahrenheit);
}
}
#[test]
fn fahrenheit_to_celsius() {
for (celsius, fahrenheit) in CASES {
let Celsius(actual) = Fahrenheit(fahrenheit).into();
assert_eq!(actual.round(), celsius);
}
}
}