Working with units can be very error prone. If one person is working in feet and one person is working in meters, what happens?
Doing all of your math in raw numerical types can be unsafe and downright confusing. What can we do to help?
Working in typed measurements increases safety by dealing with what you really care about: units of measure.
Conversions to and from different units are simple, and operator overrides allow you to work with the measurements directly.
In your Cargo.toml add the dependency...
[dependencies]
measurements = "^0.2.1"
In your code...
```rust extern crate measurements;
use measurements::{Length, Temperature, Weight, Volume};
// Lengths! let footballfield = Length::fromyards(100.0); let meters = footballfield.asmeters(); println!("There are {} meters in a football field.", meters);
/// Temperatures! let boilingwater = Temperature::fromcelsius(100.0); let fahrenheit = boilingwater.asfahrenheit(); println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
// Weights! let metricton = Weight::frommetrictons(1.0); let unitedstatestons = metricton.asshorttons(); let unitedstatespounds = metricton.aspounds(); println!("One metric ton is {} U.S. tons - that's {} pounds!", unitedstatestons, unitedstatespounds);
// Volumes! let gallon = Volume::fromgallons(1.0); let pint = Volume::frompints(1.0); let beers = gallon / pint; println!("A gallon of beer will pour {} pints!", beers); ```
References
I am by no means a measurement or math expert, I simply wanted to do something useful while learning Rust. Thank you to these sites and their authors for the great reference material used in building this library.