# convert-units
[](#contributors-)
[](https://crates.io/crates/convert-units)
[](https://docs.rs/convert-units)
[](https://github.com/trananhtung/convert-units/actions/workflows/ci.yml)
[](#license)
**Convert between units of measurement** — length, area, mass, volume, temperature, time,
digital, speed, pressure, energy, power, frequency, angle, and more (**23 measures, 185
units**), with a fluent API.
A faithful Rust port of the popular
[`convert-units`](https://www.npmjs.com/package/convert-units) npm package.
- **Zero dependencies**
- `to_best` to pick the most readable unit
- `describe`, `list`, `measures`, and `possibilities` introspection
- Differential-tested against the reference `convert-units` implementation (all 185 × 185
unit pairs)
## Install
```toml
[dependencies]
convert-units = "0.1"
```
## Usage
```rust
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);
assert_eq!(convert(1.0).from("GB").unwrap().to("MB").unwrap(), 1024.0);
// Cross-measure conversions are rejected:
assert!(convert(1.0).from("kg").unwrap().to("m").is_err());
```
Pick the most appropriate unit:
```rust
use convert_units::convert;
let best = convert(12000.0).from("mm").unwrap().to_best().unwrap();
assert_eq!((best.val, best.unit), (12.0, "m"));
```
Introspect the available units:
```rust
use convert_units::{convert, describe, measures, list};
assert_eq!(describe("kg").unwrap().singular, "Kilogram");
assert_eq!(measures().len(), 23);
assert_eq!(list(Some("length")).len(), 9);
assert_eq!(convert(1.0).from("kg").unwrap().possibilities(), ["mcg", "mg", "g", "kg", "mt", "oz", "lb", "t"]);
```
## Contributors ✨
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/trananhtung"><img src="https://avatars.githubusercontent.com/u/30992229?v=4?s=100" width="100px;" alt="Tung Tran"/><br /><sub><b>Tung Tran</b></sub></a><br /><a href="https://github.com/trananhtung/./commits?author=trananhtung" title="Code">💻</a> <a href="#maintenance-trananhtung" title="Maintenance">🚧</a></td>
</tr>
</tbody>
</table>
## License
Licensed under either of [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE) at your option.