user2039

Building a Generic Unit Conversion Tool in Rust

Nov 10th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.79 KB | Source Code | 0 0
  1. use std::fmt::Display;
  2.  
  3. // Define a trait for unit conversions
  4. trait Unit: Display {
  5.     fn convert(&self, target_unit: &Self) -> f64;
  6.     fn as_base_unit(&self) -> f64;
  7.     fn from_base_unit(value: f64) -> Self;
  8. }
  9.  
  10. // Implement the Unit trait for Length units
  11. #[derive(Debug, Copy, Clone)]
  12. enum LengthUnit {
  13.     Meter(f64),
  14.     Kilometer(f64),
  15.     Mile(f64),
  16.     Foot(f64),
  17. }
  18.  
  19. impl Unit for LengthUnit {
  20.     fn convert(&self, target_unit: &Self) -> f64 {
  21.         let base_value = self.as_base_unit();
  22.         match target_unit {
  23.             LengthUnit::Meter(_) => base_value,
  24.             LengthUnit::Kilometer(_) => base_value / 1000.0,
  25.             LengthUnit::Mile(_) => base_value / 1609.34,
  26.             LengthUnit::Foot(_) => base_value / 0.3048,
  27.         }
  28.     }
  29.  
  30.     fn as_base_unit(&self) -> f64 {
  31.         match *self {
  32.             LengthUnit::Meter(value) => value,
  33.             LengthUnit::Kilometer(value) => value * 1000.0,
  34.             LengthUnit::Mile(value) => value * 1609.34,
  35.             LengthUnit::Foot(value) => value * 0.3048,
  36.         }
  37.     }
  38.  
  39.     fn from_base_unit(value: f64) -> Self {
  40.         LengthUnit::Meter(value)
  41.     }
  42. }
  43.  
  44. impl Display for LengthUnit {
  45.     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  46.         let (value, unit_str) = match *self {
  47.             LengthUnit::Meter(value) => (value, "m"),
  48.             LengthUnit::Kilometer(value) => (value, "km"),
  49.             LengthUnit::Mile(value) => (value, "mi"),
  50.             LengthUnit::Foot(value) => (value, "ft"),
  51.         };
  52.         write!(f, "{} {}", value, unit_str)
  53.     }
  54. }
  55.  
  56. // Implement the Unit trait for Temperature units
  57. #[derive(Debug, Copy, Clone)]
  58. enum TemperatureUnit {
  59.     Celsius(f64),
  60.     Fahrenheit(f64),
  61.     Kelvin(f64),
  62. }
  63.  
  64. impl Unit for TemperatureUnit {
  65.     fn convert(&self, target_unit: &Self) -> f64 {
  66.         let base_value = self.as_base_unit();
  67.         match target_unit {
  68.             TemperatureUnit::Celsius(_) => base_value - 273.15,
  69.             TemperatureUnit::Fahrenheit(_) => (base_value - 273.15) * 9.0 / 5.0 + 32.0,
  70.             TemperatureUnit::Kelvin(_) => base_value,
  71.         }
  72.     }
  73.  
  74.     fn as_base_unit(&self) -> f64 {
  75.         match *self {
  76.             TemperatureUnit::Celsius(value) => value + 273.15,
  77.             TemperatureUnit::Fahrenheit(value) => (value - 32.0) * 5.0 / 9.0 + 273.15,
  78.             TemperatureUnit::Kelvin(value) => value,
  79.         }
  80.     }
  81.  
  82.     fn from_base_unit(value: f64) -> Self {
  83.         TemperatureUnit::Kelvin(value)
  84.     }
  85. }
  86.  
  87. impl Display for TemperatureUnit {
  88.     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  89.         let (value, unit_str) = match *self {
  90.             TemperatureUnit::Celsius(value) => (value, "°C"),
  91.             TemperatureUnit::Fahrenheit(value) => (value, "°F"),
  92.             TemperatureUnit::Kelvin(value) => (value, "K"),
  93.         };
  94.         write!(f, "{} {}", value, unit_str)
  95.     }
  96. }
  97.  
  98. // Generic function to perform unit conversion
  99. fn convert_unit<T: Unit>(value: T, target_unit: T) -> f64 {
  100.     value.convert(&target_unit)
  101. }
  102.  
  103. fn main() {
  104.     let length = LengthUnit::Mile(1.0);
  105.     let target_length = LengthUnit::Kilometer(0.0);
  106.     let converted_length = convert_unit(length, target_length);
  107.     println!(
  108.         "{} is equal to {:.2} km",
  109.         length, converted_length
  110.     );
  111.  
  112.     let temp = TemperatureUnit::Fahrenheit(98.6);
  113.     let target_temp = TemperatureUnit::Celsius(0.0);
  114.     let converted_temp = convert_unit(temp, target_temp);
  115.     println!(
  116.         "{} is equal to {:.2} °C",
  117.         temp, converted_temp
  118.     );
  119.  
  120.     let temp_kelvin = TemperatureUnit::Kelvin(0.0);
  121.     let converted_temp_kelvin = convert_unit(temp, temp_kelvin);
  122.     println!(
  123.         "{} is equal to {:.2} K",
  124.         temp, converted_temp_kelvin
  125.     );
  126. }
  127.  
Tags: rust cp6
Advertisement
Add Comment
Please, Sign In to add comment