The Measurements crate in Rust: Computing with physical quantities!

Jonathan 'theJPster' Pallant

The Measurements crate in Rust: Computing with physical quantities!

Brought to you by Jonathan 'theJPster' Pallant

Measuring things...

Mars Climate Orbiter

Mars Climate Orbiter

People sometimes make errors. The problem here was not the error, it was the failure of NASA's systems engineering, and the checks and balances in our processes to detect the error. That's why we lost the spacecraft.

Dr. Edward Weiler, NASA's Associate Administrator for Space Science

Programming in C

Why are these mistakes so easy to make, even NASA can get it wrong?

			/* Returns speed in metres/second */
			float calculate_speed(const data_t* p_data)
			{
			    ...
			}
		

Programming in C

Why are these mistakes so easy to make, even NASA can get it wrong?

			#include <speed/speed.h>
			void some_function(void)
			{
			    data_t* p_data = get_data();
			    /* Speed is in inches/second */
			    float speed = calculate_speed(p_data);
			    ...
			}
		

Python

It's even worse in Python!

			def calculate_speed(data):
			    """Calculates speed.
			    
			    Returns metres/second.
			    """
		

Python

Programming in C++

But C++ is here to save us, right?

Boost has a comprehensive units library...

			/* Returns a speed */
			units::quantity<si::velocity> calculate_speed(const Data& data)
			{
			    ....
			    auto result = 2.0 * si::meters / si::second;
			    return result;
			}
		

Rust

What is Rust?

Measurements

See https://crates.io/crates/measurements

			/* Returns a speed */
			fn calculate_speed(data: &Data) -> Speed 
			{
			    ....
			    let result = Length::from_metres(1.0) / Duration::new(1, 0);
			    return result;
			}
		

Measurements

It currently supports...

Measurements

...and...

SenseHAT

SenseHAT

Example code...

			extern crate sensehat;
			fn main() {
			    let mut sense_hat = sensehat::SenseHat::new().unwrap();
			    let pressure = sense_hat.get_pressure().unwrap();
			    println!("The pressure is {}", pressure);
			}
		

SenseHAT

Try the SenseHAT workshop (https://github.com/thejpster/pi-workshop-rs)

Send me some pull requests...

In Summary

Space probes are expensive.

Be careful when representing physical quantities.

Consider a strongly typed language.

Rust is not scary.

SenseHATs are cool.

Any Questions?

Find me at keybase.io/thejpster