diff options
| author | sam-anthony <samanthony6@protonmail.com> | 2022-01-15 20:31:04 -0330 |
|---|---|---|
| committer | sam-anthony <samanthony6@protonmail.com> | 2022-01-15 20:31:04 -0330 |
| commit | ef3980225a0a0ed5bea0d748709c1f227e33c851 (patch) | |
| tree | eafe6e82c02224ea62b5c7acdf75ae2fd93f2187 /src/mass.rs | |
| download | volute-ef3980225a0a0ed5bea0d748709c1f227e33c851.zip | |
units of measurement
Diffstat (limited to 'src/mass.rs')
| -rw-r--r-- | src/mass.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/mass.rs b/src/mass.rs new file mode 100644 index 0000000..9c57f16 --- /dev/null +++ b/src/mass.rs @@ -0,0 +1,31 @@ +pub struct Mass(f64); // Base unit is grams + +impl Mass { + /* constructors */ + pub fn from_grams(grams: f64) -> Mass { + Mass(grams) + } + + pub fn from_kilograms(kilos: f64) -> Mass { + Mass(kilos / 1000.) + } + + pub fn from_moles(moles: f64, molar_mass: f64) -> Mass { + let kilos = moles * molar_mass; + Mass::from_kilograms(kilos) + } + + /* metric */ + pub fn as_grams(&self) -> f64 { + self.0 + } + + pub fn as_kilograms(&self) -> f64 { + self.0 / 1000. + } + + /* imperial */ + pub fn as_pounds(&self) -> f64 { + self.0 * 0.002204623 + } +} |