Expand description
§iridium-units
A high-performance runtime dimensional analysis library for Rust.
This library provides physical units and quantities with automatic dimensional analysis at runtime. It supports SI, CGS, and astrophysical unit systems, as well as equivalencies for converting between different physical domains (e.g., wavelength to frequency).
§Quick Start
use iridium_units::prelude::*;
// Create quantities by multiplying values with units
// Units are const values — use them directly
let distance = 100.0 * M;
let time = 9.58 * S;
let speed = &distance / &time;
// Convert between units
let speed_kmh = speed.to(&(KM / H)).unwrap();
println!("{}", speed_kmh); // ~37.58 km / h
// Dimensional analysis is automatic
let energy = 10.0 * KG * M.pow(2) / S.pow(2);
let in_joules = energy.to(J).unwrap();§Unit Systems
The library provides units from multiple systems:
- SI units: meter, second, kilogram, ampere, kelvin, etc.
- CGS units: centimeter, gram, dyne, erg, etc.
- Astrophysical units: parsec, AU, solar mass, light year, etc.
- Imperial units: mile, foot, inch, pound, etc.
§Parsing Units and Quantities
Units and quantities can be parsed from strings with flexible syntax support:
use iridium_units::prelude::*;
use std::str::FromStr;
// Parse units from strings
let meter = Unit::from_str("m").unwrap();
let velocity_unit = Unit::from_str("km/s").unwrap();
let accel_unit = Unit::from_str("m/s^2").unwrap();
// Parse quantities (value + unit)
let distance: Quantity = "100 km".parse().unwrap();
let speed: Quantity = "9.8 m/s^2".parse().unwrap();
// Unicode and technical formats are supported
let area = parse_unit("m²").unwrap(); // Unicode superscript
let wavelength = parse_unit("µm").unwrap(); // Unicode micro
let flux = parse_unit("erg/cm²/s").unwrap(); // Astrophysical
let latex = parse_unit("kg m^{2} / s^{2}").unwrap(); // LaTeX braces
let natural = parse_unit("km per hour").unwrap(); // Natural language
let astro = parse_unit("M_sun").unwrap(); // Astrophysical subscripts
let parens = parse_unit("(kg m)/s^2").unwrap(); // ParenthesesSee the parsing module for comprehensive documentation of supported formats.
§Custom Unit Registry
For applications needing custom units (e.g., loaded from a database):
use iridium_units::prelude::*;
let registry = UnitRegistry::with_builtins()
.with_unit(&["my_unit", "mu"], Unit::from(M));
let unit = registry.parse_unit("my_unit").unwrap();§Equivalencies
Some physical quantities can be converted through physical laws even though they have different dimensions:
use iridium_units::prelude::*;
use iridium_units::equivalencies::spectral;
let wavelength = 500.0 * NM;
let frequency = wavelength.to_equiv(HZ, spectral()).unwrap();Re-exports§
pub use dimension::Dimension;pub use dimension::Rational16;pub use error::UnitError;pub use error::UnitResult;pub use quantity::batch_convert;pub use quantity::batch_convert_into;pub use quantity::conversion_factor;pub use quantity::Quantity;pub use unit::base::BaseUnit;pub use unit::Unit;pub use parsing::lookup_unit;pub use parsing::parse_quantity;pub use parsing::parse_unit;pub use parsing::register_unit;pub use parsing::UnitRegistry;
Modules§
- constants
- Physical constants.
- dimension
- Dimensional analysis types.
- equivalencies
- Equivalencies for converting between different physical domains.
- error
- Error types for the iridium-units library.
- parsing
- String parsing for units and quantities.
- prelude
- Prelude module for convenient imports.
- quantity
- Physical quantities with values and units.
- systems
- Unit systems.
- unit
- Unit types and operations.