Skip to main content

Crate convert_units

Crate convert_units 

Source
Expand description

§convert-units — convert between units of measurement

A fluent unit converter covering length, area, mass, volume, temperature, time, digital, speed, pressure, energy, power, frequency, angle, and more (23 measures, 185 units). A faithful Rust port of the popular convert-units npm package.

use convert_units::convert;

assert_eq!(convert(1.0).from("kg").unwrap().to("lb").unwrap(), 2.2046244201837775);
assert_eq!(convert(100.0).from("cm").unwrap().to("m").unwrap(), 1.0);
assert_eq!(convert(0.0).from("C").unwrap().to("F").unwrap(), 32.0);

Find the most appropriate unit with Conversion::to_best:

use convert_units::convert;
let best = convert(12000.0).from("mm").unwrap().to_best().unwrap();
assert_eq!(best.unit, "m");
assert_eq!(best.val, 12.0);

Zero dependencies.

Structs§

Best
The best matching unit found by Conversion::to_best.
Conversion
A value with a known source unit, ready to convert.
Convert
A value awaiting its source unit.
UnitInfo
Metadata describing a unit.

Enums§

Error
An error from a conversion.

Functions§

convert
Start a conversion of value. Call Convert::from next.
describe
Describe a single unit by its abbreviation, or None if unknown.
list
A detailed list of all supported units, optionally restricted to one measure.
measures
The names of all supported measures, in order.