Crates.io | measurements |
lib.rs | measurements |
version | 0.11.0 |
source | src |
created_at | 2015-06-17 02:14:37.778841 |
updated_at | 2022-02-06 19:44:47.778526 |
description | Handle metric, imperial, and other measurements with ease! Types: Length, Temperature, Weight, Volume, Pressure |
homepage | |
repository | https://github.com/rust-embedded-community/rust-measurements |
max_upload_size | |
id | 2403 |
size | 234,904 |
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.11"
In your code...
extern crate measurements;
use measurements::{Length, Pressure, Temperature, Volume, Weight};
fn main() {
// Lengths!
let football_field = Length::from_yards(100.0);
let meters = football_field.as_meters();
println!("There are {} meters in a football field.", meters);
/// Temperatures!
let boiling_water = Temperature::from_celsius(100.0);
let fahrenheit = boiling_water.as_fahrenheit();
println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
// Weights!
let metric_ton = Weight::from_metric_tons(1.0);
let united_states_tons = metric_ton.as_short_tons();
let united_states_pounds = metric_ton.as_pounds();
println!("One metric ton is {} U.S. tons - that's {} pounds!", united_states_tons, united_states_pounds);
// Volumes!
let gallon = Volume::from_gallons(1.0);
let pint = Volume::from_pints(1.0);
let beers = gallon / pint;
println!("A gallon of beer will pour {:.1} pints!", beers);
// Pressures!
let atmosphere = Pressure::from_atmospheres(1.0);
println!("Earth's atmosphere is usually {} psi", atmosphere.as_psi());
}
The crate contains few features to disable or enable certain functionalities:
std
libm
for some math functionsReferences
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.