# 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 ## 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.`
`    """`
```

## 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;`
`}`
```

What is Rust?

## 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...

• Acceleration
• Angle
• Angular Velocity
• Area
• Data
• Energy
• Force
• Frequency

...and...

• Length
• Mass
• Power
• Pressure
• Speed
• Temperature
• Torque
• Volume

## SenseHAT

• A driver for the SenseHAT
• Supports Temperature (both), Pressure and Humidity sensors
• Uses the Measurements crate

## 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