Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples #455

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you duplicate these changes in lib.rs (#265 will eventually resolve this duplication).

Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,33 @@ operations:
extern crate uom;

use uom::si::f32::*;
use uom::si::length::kilometer;
use uom::si::time::second;
use uom::si::length::{kilometer, meter};
use uom::si::time::{minute, second};
use uom::si::velocity::meter_per_second;

fn main() {
// Create quantities
let length = Length::new::<kilometer>(5.0);
let time = Time::new::<second>(15.0);
let velocity/*: Velocity*/ = length / time;
let _acceleration = calc_acceleration(velocity, time);
//let error = length + time; // error[E0308]: mismatched types

// Perfrom unit conversions
assert_eq!(length.get::<meter>(), 5_000.0);
assert_eq!(time.get::<minute>(), 0.25);

// Setup re-usable format arguments for quantities
let m = Length::format_args(meter, Abbreviation);
let s = Time::format_args(second, Abbreviation);

// Print results of simple formulas using different output units.
println!(
"{} / {} = {}",
m.with(length),
s.with(time),
velocity.into_format_args(meter_per_second, Abbreviation) // One-off quantity formatting.
);
}

fn calc_acceleration(velocity: Velocity, time: Time) -> Acceleration {
Expand Down
11 changes: 8 additions & 3 deletions examples/si.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

use uom::fmt::DisplayStyle::Abbreviation;
use uom::si::f32::*;
use uom::si::length::{centimeter, kilometer, meter};
use uom::si::time::second;
use uom::si::length::{centimeter, kilometer, meter, millimeter};
use uom::si::time::{minute, second};
use uom::si::velocity::{kilometer_per_second, meter_per_second};

fn main() {
// Setup length and time quantities using different units.
let l1 = Length::new::<meter>(15.0);
let l2 = Length::new::<centimeter>(10.0);
let t1 = Time::new::<second>(50.0);
let t1 = Time::new::<second>(60.0);
let v1 = l1 / t1;
//let error = l1 + t1; // error[E0308]: mismatched types

// Perfrom unit conversions
assert_eq!(l1.get::<centimeter>().round(), 1_500.0);
assert_eq!(l2.get::<millimeter>().round(), 100.0);
assert_eq!(t1.get::<minute>().round(), 1.0);

// Setup re-usable format arguments.
let m = Length::format_args(meter, Abbreviation);
let cm = Length::format_args(centimeter, Abbreviation);
Expand Down
Loading