From ae821746f3483299d952fe6e74ff44ec59d1f7bf Mon Sep 17 00:00:00 2001 From: Mike Boutin Date: Wed, 29 Jul 2020 16:12:10 -0400 Subject: [PATCH] Change examples to use `Display` instead of `Debug`. mks and si examples are updated to use `format_args` or `into_format_args` as appropriate so that the `Display` trait can be used to display quantities in specific units. Part of #123. --- examples/mks.rs | 9 ++++----- examples/si.rs | 52 ++++++++++++++++++++----------------------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/examples/mks.rs b/examples/mks.rs index e2a923c7..71d5a3be 100644 --- a/examples/mks.rs +++ b/examples/mks.rs @@ -4,16 +4,15 @@ extern crate uom; use length::{foot, meter}; +use uom::fmt::DisplayStyle::Abbreviation; fn main() { let l1 = f32::Length::new::(100.0); println!( - "{:?} {} = {:?} {}", - l1.get::(), - meter::abbreviation(), - l1.get::(), - foot::abbreviation() + "{} = {}", + l1.into_format_args(meter, Abbreviation), + l1.into_format_args(foot, Abbreviation) ); } diff --git a/examples/si.rs b/examples/si.rs index 816fc30c..a79412ce 100644 --- a/examples/si.rs +++ b/examples/si.rs @@ -2,53 +2,43 @@ extern crate uom; +use uom::fmt::DisplayStyle::Abbreviation; use uom::si::f32::*; use uom::si::length::{centimeter, kilometer, meter}; use uom::si::time::second; use uom::si::velocity::{kilometer_per_second, meter_per_second}; -use uom::si::Unit; fn main() { + // Setup length and time quantities using different units. let l1 = Length::new::(15.0); let l2 = Length::new::(10.0); let t1 = Time::new::(50.0); let v1 = l1 / t1; //let error = l1 + t1; // error[E0308]: mismatched types + // Setup re-usable format arguments. + let m = Length::format_args(meter, Abbreviation); + let cm = Length::format_args(centimeter, Abbreviation); + let s = Time::format_args(second, Abbreviation); + + // Print results of simple formulas using different output units. + println!("{} + {} = {}", m.with(l1), cm.with(l2), m.with(l1 + l2)); println!( - "{:?} {} + {:?} {} = {:?} {}", - l1.get::(), - meter::abbreviation(), - l2.get::(), - centimeter::abbreviation(), - (l1 + l2).get::(), - meter::abbreviation() - ); - println!( - "{:?} {} + {:?} {} = {:?} {}", - l1.get::(), - meter::abbreviation(), - l2.get::(), - centimeter::abbreviation(), - (l1 + l2).get::(), - kilometer::abbreviation() + "{} + {} = {}", + m.with(l1), + cm.with(l2), + (l1 + l2).into_format_args(kilometer, Abbreviation) ); println!( - "{:?} {} / {:?} {} = {:?} {}", - l1.get::(), - meter::abbreviation(), - t1.get::(), - second::abbreviation(), - v1.get::(), - meter_per_second::abbreviation() + "{} / {} = {}", + m.with(l1), + s.with(t1), + v1.into_format_args(meter_per_second, Abbreviation) ); println!( - "{:?} {} / {:?} {} = {:?} {}", - l1.get::(), - meter::abbreviation(), - t1.get::(), - second::abbreviation(), - v1.get::(), - kilometer_per_second::abbreviation() + "{} / {} = {}", + m.with(l1), + s.with(t1), + v1.into_format_args(kilometer_per_second, Abbreviation) ); }