eng-units-0.1.3 has been yanked.
eng-units
Rust library to build, calculate and convert custom engineering units.
This is a work in process!
Only a few units have been implemented for development purposes!
Feel free to review and contribute on the repository!
Example Usage
Making a EngUnit
Example Code
println!;
let mut unit = new;
unit.value = 9.81; // <-- Value of the unit
unit.label = "Test Unit".to_string; // <-- Optional label used for identification
unit.push_unit; // <-- Pushes meter to the numerator
unit.push_unit; // <-- Pushes sec^2 to the denominator
println!; // <-- It worked!
Output
Let's make our first EngUnit!
Test Unit: 9.81 (m)/(sec^2)
Using EngUnit
unit.label; // <-- The label used for this unit
unit.value; // <-- The value of the unit as f64
unit.units; // <-- String representation of the units
unit.has_name; // <-- Checks if this unit has a known unit type
unit.unit_name; // <-- String representation of the unit name (ex. acceleration)
Example Code
println!;
println!;
println!;
println!;
println!;
Output
Test Unit has a common name: true
Test Unit is a unit of acceleration
Test Unit value: 9.81
Test Unit units: (m)/(sec^2)
Converting Units
Example Code
println!;
unit.change_unit; // <-- Changes the length unit to km
unit.change_unit; // <-- Changes the time unit to hr
println!; // <-- Value is calculated automatically!
Output
Converting Test Unit to km/hr^2
Test Unit: 127137.6 (km)/(hr^2)
Adding Units
Example Code
println!;
println!;
println!;
let mut unit_1 = new;
unit_1.value = 12.0;
unit_1.push_unit;
let mut unit_2 = new;
unit_2.value = 40.0;
unit_2.push_unit;
let mut unit_3 = unit_1.clone + unit_2.clone;
unit_3.label = "Result".to_string;
println!;
println!;
println!;
println!;
println!;
println!;
println!;
println!;
Output
Adding and subtracting units is easy!
The units don't need to be the same...
But they do require the same fundamental dimension!
12 cm^3 + 40 mm^3 = Result: 12.04 cm^3
Result has a common name: true
Result is a unit of volume
Result value: 12.04
Result units: cm^3
AddAssign
println!;
let mut u1 = new;
u1.value = 1.0;
u1.push_unit;
let mut u2 = new;
u2.value = 553.0;
u2.push_unit;
u1 += u2;
println!;
Output
AddAssign operators work too!
1.553 km
Example Error
Adding incompatible units (different fundamental dimmension) will raise a panic!
println!;
let mut unit_1 = new;
unit_1.value = 12.0;
unit_1.push_unit; // <-- L^3
let mut unit_2 = new;
unit_2.value = 40.0;
unit_2.push_unit; // <-- L^2
let mut unit_3 = unit_1.clone + unit_2.clone; // <-- L^3 + L^2 = ??
Output
thread 'main' panicked at 'Tried to add incomaptible units (length)'
License
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.