simple_units
A simple unit system for Rust.
It allows you to write numeric code that is not only type safe but also "unit safe".
It works on stable Rust, but unfortunately that means it doesn't scale. All units (and combination of them) must be written by hand. Macros help (and maybe some more macro magic can help more) but it is still a lot of work.
For me it already payed off: I found a small bug in an old Fortran program we are using ;-)
So if your favorite unit is missing just let me know.
There already is a better unit system for Rust: Dimensioned, needs at least Rust 1.15.
And another one here: runits and here: uom
There is also a crate to help you convert between units: rink-rs
Notes
There are some blog posts / notes about unit systems in Rust:
https://www.reddit.com/r/rust/comments/37qut9/typesafe_userdefined_units_of_measure_for_rust/
https://blog.mozilla.org/research/2014/06/23/static-checking-of-units-in-servo/
https://github.com/jaheba/stuff/blob/master/communicating_intent.md
https://www.reddit.com/r/rust/comments/5uacxs/types_units_and_quantities/
Examples:
extern crate simple_units;
use *;
Conversion is also supported:
extern crate simple_units;
use *;
use *;
It would be nice if Rust would allow to implement the as
operator. Then you could write s.th. like this:
extern crate simple_units;
use *;
use *;
I have plans for a better unit system, based on build.rs and comments: Basically that means to scan all *.rs Rust source files and evaluate the expressions that have a special comment:
Note the special #[]
notation to describe units. Between the comment begin token and the unit description only space and tabs will be allowed. After that you can place any comments. Otherwise the unit description will be ignored as in the example above.
This will work on stable Rust.