@@ -18,126 +18,126 @@ const METER_MILE_FACTOR: f64 = 0.000621371192237;
1818
1919/// The `Length` struct can be used to deal with lengths in a common way.
2020/// Common metric and imperial units are supported.
21- ///
21+ ///
2222/// # Example
23- ///
23+ ///
2424/// ```
2525/// use measurements::Length;
26- ///
26+ ///
2727/// let football_field = Length::from_yards(100.0);
2828/// let meters = football_field.as_meters();
2929/// println!("There are {} meters in a football field.", meters);
3030/// ```
3131#[ derive( Copy , Clone , Debug ) ]
3232pub struct Length {
33- meters : f64
33+ meters : f64 ,
3434}
3535
3636impl Length {
3737 // Inputs, metric
3838 pub fn from_meters ( meters : f64 ) -> Self {
3939 Length { meters : meters }
4040 }
41-
41+
4242 pub fn from_nanometers ( nanometers : f64 ) -> Self {
4343 Self :: from_meters ( nanometers / METER_NANOMETER_FACTOR )
4444 }
45-
45+
4646 pub fn from_micrometers ( micrometers : f64 ) -> Self {
4747 Self :: from_meters ( micrometers / METER_MICROMETER_FACTOR )
4848 }
49-
49+
5050 pub fn from_millimeters ( millimeters : f64 ) -> Self {
5151 Self :: from_meters ( millimeters / METER_MILLIMETER_FACTOR )
5252 }
53-
53+
5454 pub fn from_centimeters ( centimeters : f64 ) -> Self {
5555 Self :: from_meters ( centimeters / METER_CENTIMETER_FACTOR )
5656 }
57-
57+
5858 pub fn from_decameters ( decameters : f64 ) -> Self {
5959 Self :: from_meters ( decameters / METER_DECAMETER_FACTOR )
6060 }
61-
61+
6262 pub fn from_hectometers ( hectometers : f64 ) -> Self {
6363 Self :: from_meters ( hectometers / METER_HECTOMETER_FACTOR )
6464 }
65-
65+
6666 pub fn from_kilometers ( kilometers : f64 ) -> Self {
6767 Self :: from_meters ( kilometers / METER_KILOMETER_FACTOR )
6868 }
69-
69+
7070 // Inputs, imperial
7171 pub fn from_inches ( inches : f64 ) -> Self {
7272 Self :: from_meters ( inches / METER_INCH_FACTOR )
7373 }
74-
74+
7575 pub fn from_feet ( feet : f64 ) -> Self {
7676 Self :: from_meters ( feet / METER_FEET_FACTOR )
7777 }
78-
78+
7979 pub fn from_yards ( yards : f64 ) -> Self {
8080 Self :: from_meters ( yards / METER_YARD_FACTOR )
8181 }
82-
82+
8383 pub fn from_furlongs ( furlongs : f64 ) -> Self {
8484 Self :: from_meters ( furlongs / METER_FURLONG_FACTOR )
8585 }
86-
86+
8787 pub fn from_miles ( miles : f64 ) -> Self {
8888 Self :: from_meters ( miles / METER_MILE_FACTOR )
8989 }
90-
90+
9191 // Outputs, metric
9292 pub fn as_nanometers ( & self ) -> f64 {
9393 self . meters * METER_NANOMETER_FACTOR
9494 }
95-
95+
9696 pub fn as_micrometers ( & self ) -> f64 {
9797 self . meters * METER_MICROMETER_FACTOR
9898 }
99-
99+
100100 pub fn as_millimeters ( & self ) -> f64 {
101101 self . meters * METER_MILLIMETER_FACTOR
102102 }
103-
103+
104104 pub fn as_centimeters ( & self ) -> f64 {
105105 self . meters * METER_CENTIMETER_FACTOR
106106 }
107-
107+
108108 pub fn as_meters ( & self ) -> f64 {
109109 self . meters
110110 }
111-
111+
112112 pub fn as_decameters ( & self ) -> f64 {
113113 self . meters * METER_DECAMETER_FACTOR
114114 }
115-
115+
116116 pub fn as_hectometer ( & self ) -> f64 {
117117 self . meters * METER_HECTOMETER_FACTOR
118118 }
119-
119+
120120 pub fn as_kilometers ( & self ) -> f64 {
121121 self . meters * METER_KILOMETER_FACTOR
122122 }
123-
123+
124124 // Outputs, imperial
125125 pub fn as_inches ( & self ) -> f64 {
126126 self . meters * METER_INCH_FACTOR
127127 }
128-
128+
129129 pub fn as_feet ( & self ) -> f64 {
130130 self . meters * METER_FEET_FACTOR
131131 }
132-
132+
133133 pub fn as_yards ( & self ) -> f64 {
134134 self . meters * METER_YARD_FACTOR
135135 }
136-
136+
137137 pub fn as_furlongs ( & self ) -> f64 {
138138 self . meters * METER_FURLONG_FACTOR
139139 }
140-
140+
141141 pub fn as_miles ( & self ) -> f64 {
142142 self . meters * METER_MILE_FACTOR
143143 }
@@ -147,10 +147,16 @@ impl Measurement for Length {
147147 fn get_base_units ( & self ) -> f64 {
148148 self . meters
149149 }
150-
150+
151151 fn from_base_units ( units : f64 ) -> Self {
152152 Self :: from_meters ( units)
153153 }
154154}
155155
156156implement_measurement ! { Length }
157+
158+ impl :: std:: fmt:: Display for Length {
159+ fn fmt ( & self , f : & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
160+ write ! ( f, "{:.1} m" , self . as_meters( ) )
161+ }
162+ }
0 commit comments