Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt::Display;
- // Define a trait for unit conversions
- trait Unit: Display {
- fn convert(&self, target_unit: &Self) -> f64;
- fn as_base_unit(&self) -> f64;
- fn from_base_unit(value: f64) -> Self;
- }
- // Implement the Unit trait for Length units
- #[derive(Debug, Copy, Clone)]
- enum LengthUnit {
- Meter(f64),
- Kilometer(f64),
- Mile(f64),
- Foot(f64),
- }
- impl Unit for LengthUnit {
- fn convert(&self, target_unit: &Self) -> f64 {
- let base_value = self.as_base_unit();
- match target_unit {
- LengthUnit::Meter(_) => base_value,
- LengthUnit::Kilometer(_) => base_value / 1000.0,
- LengthUnit::Mile(_) => base_value / 1609.34,
- LengthUnit::Foot(_) => base_value / 0.3048,
- }
- }
- fn as_base_unit(&self) -> f64 {
- match *self {
- LengthUnit::Meter(value) => value,
- LengthUnit::Kilometer(value) => value * 1000.0,
- LengthUnit::Mile(value) => value * 1609.34,
- LengthUnit::Foot(value) => value * 0.3048,
- }
- }
- fn from_base_unit(value: f64) -> Self {
- LengthUnit::Meter(value)
- }
- }
- impl Display for LengthUnit {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- let (value, unit_str) = match *self {
- LengthUnit::Meter(value) => (value, "m"),
- LengthUnit::Kilometer(value) => (value, "km"),
- LengthUnit::Mile(value) => (value, "mi"),
- LengthUnit::Foot(value) => (value, "ft"),
- };
- write!(f, "{} {}", value, unit_str)
- }
- }
- // Implement the Unit trait for Temperature units
- #[derive(Debug, Copy, Clone)]
- enum TemperatureUnit {
- Celsius(f64),
- Fahrenheit(f64),
- Kelvin(f64),
- }
- impl Unit for TemperatureUnit {
- fn convert(&self, target_unit: &Self) -> f64 {
- let base_value = self.as_base_unit();
- match target_unit {
- TemperatureUnit::Celsius(_) => base_value - 273.15,
- TemperatureUnit::Fahrenheit(_) => (base_value - 273.15) * 9.0 / 5.0 + 32.0,
- TemperatureUnit::Kelvin(_) => base_value,
- }
- }
- fn as_base_unit(&self) -> f64 {
- match *self {
- TemperatureUnit::Celsius(value) => value + 273.15,
- TemperatureUnit::Fahrenheit(value) => (value - 32.0) * 5.0 / 9.0 + 273.15,
- TemperatureUnit::Kelvin(value) => value,
- }
- }
- fn from_base_unit(value: f64) -> Self {
- TemperatureUnit::Kelvin(value)
- }
- }
- impl Display for TemperatureUnit {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- let (value, unit_str) = match *self {
- TemperatureUnit::Celsius(value) => (value, "°C"),
- TemperatureUnit::Fahrenheit(value) => (value, "°F"),
- TemperatureUnit::Kelvin(value) => (value, "K"),
- };
- write!(f, "{} {}", value, unit_str)
- }
- }
- // Generic function to perform unit conversion
- fn convert_unit<T: Unit>(value: T, target_unit: T) -> f64 {
- value.convert(&target_unit)
- }
- fn main() {
- let length = LengthUnit::Mile(1.0);
- let target_length = LengthUnit::Kilometer(0.0);
- let converted_length = convert_unit(length, target_length);
- println!(
- "{} is equal to {:.2} km",
- length, converted_length
- );
- let temp = TemperatureUnit::Fahrenheit(98.6);
- let target_temp = TemperatureUnit::Celsius(0.0);
- let converted_temp = convert_unit(temp, target_temp);
- println!(
- "{} is equal to {:.2} °C",
- temp, converted_temp
- );
- let temp_kelvin = TemperatureUnit::Kelvin(0.0);
- let converted_temp_kelvin = convert_unit(temp, temp_kelvin);
- println!(
- "{} is equal to {:.2} K",
- temp, converted_temp_kelvin
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment