gnu-units
Safe Rust bindings for the GNU units conversion library.
This crate provides a high-level Rust API for dimensional analysis and unit conversion. By default it uses a pure-Rust engine with no C dependency. Optionally, a vendored GNU units C library backend is available.
Features
- Parse and convert between thousands of units (length, mass, time, currency, etc.)
- Dimensionless factor extraction
- Conformability checking between units
- List all known unit definitions
- Optional Rust-native currency rate updates (
currency-updatefeature)
Usage
[]
= "0.2"
use ;
// Simple conversion factor
let factor = convert.unwrap;
assert!;
// Conversion with a value
let km_val = 5.0;
let miles_val = km_val * convert.unwrap;
// Parse and inspect a unit
let unit = parse.unwrap;
println!; // "kg m / s s"
// Check conformability
let a = parse.unwrap;
let b = parse.unwrap;
assert!;
// Find all conformable units
let lengths = conformable.unwrap;
assert!;
// Error handling with ErrorCode
let err = parse.unwrap_err;
match err.code
Optional features
| Feature | Description |
|---|---|
native (default) |
Pure-Rust unit engine, no C dependency |
vendored |
Build and statically link the vendored GNU units C sources |
bindgen |
Regenerate FFI bindings |
currency-update |
Enable Rust-native currency exchange rate updates |
Note:
nativeandvendoredare mutually exclusive. To use the C backend:[] = { = "0.2", = false, = ["vendored"] }
License
Licensed under GPL-3.0-or-later. The vendored GNU units source code is also GPL-3.0-or-later.