units::unit
On this page
Units: the static Unit of a known dimension (the symbols, what
.to and .value_in take) and the dynamic DynUnit that parsing
and unit algebra with run-time exponents give.
Modules
catalogueThe unit catalogue: SI base and derived units, units accepted with the SI, and common other units with their NIST SP 811 factors (exact where the definition is exact).prefixSI prefixes, quecto (1e-30) to quetta (1e30).
Structs
UnitSource
pub struct Unit<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32>A unit of a dimension fixed in its type: a symbol and the size of
one of it in coherent SI units (km is 1000 m). The dimension
parameters are those of Quantity. * and / combine units into
new ones with the right dimension; u ** n gives a DynUnit, since
n is a value (use u.squared(), u.powi::<n>(), or the symbols
m², s² to stay static).
use units::symbols::{km, h, kg, m, s²};
let kmh = km / h;
assert(kmh.symbol() == "km/h");
assert(kmh.of(36.0) == 10m / 1s);
let newton = kg * m / s²;
assert(newton.dim().symbol() == "N");
A unit with
symbolworthscalecoherent SI units; the dimension comes from the expected type.nabiuse units::Unit; let furlong: Unit<1, 0, 0, 0, 0, 0, 0> = Unit::new("fur", 201.168); assert(furlong.of(1.0) == 201.168m);
Sourcepub const fn of_quantity(§symbol: str, q: Quantity<f64, L, M, T, I, K, N, J>) -> Unit<L, M, T, I, K, N, J> A unit named
symbolthe size ofq, which also gives the dimension.nabiuse units::Unit; let furlong = Unit::of_quantity("fur", 220yd); assert(furlong.of(8.0).approx_eq(1mi));The symbol (
"km/h").nabiuse units::symbols::kPa; assert(kPa.symbol() == "kPa");One of this unit in coherent SI units.
nabiuse units::symbols::inch; assert(inch.scale() == 0.0254);The dimension, as a value.
nabiuse units::symbols::J; assert(J.dim().name() == "energy");valueof this unit, as a quantity:km.of(3.0)is3km.nabiuse units::symbols::km; assert(km.of(3.0) == 3000m);
Sourcepub fn array<const R: usize>(§self, values: Array<f64, R>) -> Quantity<Array<f64, R>, L, M, T, I, K, N, J> Each element of
values, given in this unit, as one array quantity.nabiuse core::array::Array; use units::symbols::km; let d = km.array(Array::from_list([1.0, 2.5], [2])); assert(d.at([1]) == 2.5km);The same unit with its dimension as a value.
nabiuse units::symbols::mph; assert(mph.dynamic().dim.name() == "velocity");The unit renamed:
(kg * m / s²).named("N").nabiuse units::symbols::{kg, m, s²}; assert((kg * m / s²).named("N").symbol() == "N");
Sourcepub fn squared(§self) -> Unit<{ 2 * L }, { 2 * M }, { 2 * T }, { 2 * I }, { 2 * K }, { 2 * N }, { 2 * J }> The square of the unit (
cm.squared()iscm²).nabiuse units::symbols::cm; assert(cm.squared().symbol() == "cm²");
Sourcepub fn cubed(§self) -> Unit<{ 3 * L }, { 3 * M }, { 3 * T }, { 3 * I }, { 3 * K }, { 3 * N }, { 3 * J }> The cube of the unit (
cm.cubed()iscm³).nabiuse units::symbols::{cm, mL}; assert((cm.cubed().scale() - mL.scale()).abs() < 1e-21);The reciprocal unit (
s.inv()is1/s).nabiuse units::symbols::min; assert(min.inv().of(60.0).value == 1.0);
Sourcepub fn powi<const P: i32>(§self) -> Unit<{ L * P }, { M * P }, { T * P }, { I * P }, { K * P }, { N * P }, { J * P }> The unit to the const power
P.nabiuse units::symbols::mm; assert(mm.powi::<3>().symbol() == "mm³");
Trait implementations 6
impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> Display for Unit<L, M, T, I, K, N, J>impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32, const L2: i32, const M2: i32, const T2: i32, const I2: i32, const K2: i32, const N2: i32, const J2: i32> ops::Mul<Unit<L2, M2, T2, I2, K2, N2, J2>> for Unit<L, M, T, I, K, N, J>impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32, const L2: i32, const M2: i32, const T2: i32, const I2: i32, const K2: i32, const N2: i32, const J2: i32> ops::Div<Unit<L2, M2, T2, I2, K2, N2, J2>> for Unit<L, M, T, I, K, N, J>impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Mul<DynUnit> for Unit<L, M, T, I, K, N, J>impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Div<DynUnit> for Unit<L, M, T, I, K, N, J>impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Pow<i32> for Unit<L, M, T, I, K, N, J>
DynUnitSource
pub struct DynUnitA unit whose dimension is a value: what DynUnit::parse returns and
Measure carries. offset is nonzero only for the temperature
scales (degC, degF): a value v of the unit is v * scale + offset in SI. In a product or quotient an offset unit stands for its
interval (J/(kg*degC) is J/(kg*K)).
use units::DynUnit;
let u = try! DynUnit::parse("kg*m/s^2");
assert(u.dim.symbol() == "N" && u.scale == 1.0);
let p = try! DynUnit::parse("psi");
assert((p.scale - 6894.757293168361).abs() < 1e-9);
The symbol as written or composed (
"km/h").One of the unit in coherent SI units.
The SI value of zero of the unit (273.15 for
degC).The dimension.
Parse a unit expression: symbols or names with SI prefixes (
km,µs,kilometre),*,·,or/between them, powers as^2,**2,²,⁻¹or a trailing digit (m2), parentheses, and1/(1/s). Throws aUnitErrorthat points at the bad part and suggests a known unit.nabiuse units::DynUnit; assert((try! DynUnit::parse("J/(mol·K)")).dim.name() == none); assert((try! DynUnit::parse("µm")).scale == 1e-6); assert((try! DynUnit::parse("km/h")).scale == 1.0 / 3.6);The coherent SI unit of
dim(scale 1), printed with named derived units.nabiuse units::{Dim, DynUnit}; assert(DynUnit::coherent(Dim::LENGTH * Dim::MASS / Dim::TIME.pow(2)).symbol == "N");The SI value of
vof this unit.nabiuse units::DynUnit; assert((try! DynUnit::parse("degC")).to_si(25.0) == 298.15);The value in this unit of the SI value
si.nabiuse units::DynUnit; assert(((try! DynUnit::parse("degF")).from_si(273.15) - 32.0).abs() < 1e-12);The unit as an interval: the same scale with no offset (
degCbecomes a kelvin-sized step).nabiuse units::DynUnit; assert((try! DynUnit::parse("degC")).interval().offset == 0.0);
Sourcepub fn fixed<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32>(§self) throws (UnitError) -> Unit<L, M, T, I, K, N, J> The static unit of this one, its dimension given by the expected type; throws a
UnitError::Mismatchwhen the dimensions differ (and for an offset unit, which has no static form).nabiuse units::{DynUnit, Unit}; use units::kinds::Velocity; let kn: Unit<1, 0, -1, 0, 0, 0, 0> = try! (try! DynUnit::parse("knot")).fixed(); assert(kn.of(1.0).approx_eq(1852m / 3600s));The unit renamed.
nabiuse units::DynUnit; assert((try! DynUnit::parse("kg*m/s^2")).named("N").symbol == "N");
Trait implementations 6
impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Mul<Unit<L, M, T, I, K, N, J>> for DynUnitimpl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Div<Unit<L, M, T, I, K, N, J>> for DynUnitimpl Display for DynUnitimpl ops::Mul for DynUnitimpl ops::Div for DynUnitimpl ops::Pow<i32> for DynUnit