#measurements #lengths #weights #temperatures #volumes

no-std measurements

Handle metric, imperial, and other measurements with ease! Types: Length, Temperature, Weight, Volume, Pressure

13 releases (8 breaking)

0.10.3 Oct 11, 2018
0.10.2 Mar 24, 2018
0.9.0 Mar 13, 2018
0.6.0 Oct 14, 2017
0.2.1 Jul 1, 2015

#3 in Value formatting

Download history 4732/week @ 2018-12-14 2940/week @ 2018-12-21 4695/week @ 2018-12-28 14/week @ 2019-01-04 4/week @ 2019-01-11 1/week @ 2019-01-18 5/week @ 2019-01-25 3/week @ 2019-02-01 2/week @ 2019-02-15 17/week @ 2019-02-22 29/week @ 2019-03-01 9/week @ 2019-03-08 1/week @ 2019-03-15 312/week @ 2019-03-22

154 downloads per month
Used in 1 crate

MIT license

172KB
4K SLoC

Build Status

Type-safe units of measure for Rust

Why should I care? I already have numbers...

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?

Typed measurements to the rescue!

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.

Currently available measurement types

  • Acceleration
  • Angle
  • Angular Velocity
  • Area
  • Current
  • Data (bytes, etc)
  • Energy
  • Force
  • Frequency
  • Length
  • Mass
  • Power
  • Pressure
  • Resistance
  • Speed
  • Temperature
  • Torque
  • Voltage
  • Volume

Examples

In your Cargo.toml add the dependency...

[dependencies]
measurements = "^0.9.0"

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());
}

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.

Dependencies

  • optional serde 1.0+derive