Rust

What, why

MDAP Hacky Hour

What is Rust

  • Systems programming language (like C, C++, Fortran, etc.)
  • No runtime or garbage collection (like Python, Julia, Java etc.)
  • Statically typed (i32, f64, u8, etc.)
  • Memory and thread safe
  • Fast
MDAP Hacky Hour

Safety

  • Memory safety
    • Approximately 70% if the security issues dealt with by Microsoft are memory safety related.
      • e.g. overflow, dangling pointers, races
    • Important for critical systems (and research software!)
  • "Fearless concurrency"
    • Almost guaranteed free from data races with safe code

A programmer had a problem. They thought to themselves,
"I know, I'll solve it with threads!".

have Now problems. two they

-- anon

MDAP Hacky Hour

Safety

The borrow checker

  • In Rust, ownership is tracked and checked at compile time
let mut s = String::from("hello");

let r1 = &s; // no problem
let r2 = &s; // no problem
let r3 = &mut s; // BIG PROBLEM

println!("{}, {}, and {}", r1, r2, r3);
let mut s = String::from("hello");

let r1 = &s; // no problem
let r2 = &s; // no problem
println!("{} and {}", r1, r2);
// variables r1 and r2 will not be used after this point

let r3 = &mut s; // no problem
println!("{}", r3);

Safety

The borrow checker

$ cargo run
   Compiling ownership v0.1.0 (file:///projects/ownership)
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
 --> src/main.rs:6:14
  |
4 |     let r1 = &s; // no problem
  |              -- immutable borrow occurs here
5 |     let r2 = &s; // no problem
6 |     let r3 = &mut s; // BIG PROBLEM
  |              ^^^^^^ mutable borrow occurs here
7 | 
8 |     println!("{}, {}, and {}", r1, r2, r3);
  |                                -- immutable borrow later used here

For more information about this error, try `rustc --explain E0502`.
error: could not compile `ownership` due to previous error
MDAP Hacky Hour

Why not Rust

  • It's a complicated and large language (though still way smaller than C++)
  • It takes time to learn and the early phases can be painful
  • The eco-system is relatively immature compared to older similar langauges
MDAP Hacky Hour

Why Rust

  • Enforces good practices and safety
    • Important for research accuracy and reproducibility
    • Will improve your code quality in all languages (especially other systems-level)
  • amazing tooling (package manager, compiler, linter, language-server)
  • Excellent learning resources ("the book", rustlings etc.)
  • Good standard library (everything you'd expect to see)
  • Loved by many! (e.g. most loved language in StackOverflow Developer Survey every year since 2016)
  • Passionate and welcoming community
MDAP Hacky Hour

Modern tooling

Cargo

[package]
name = "rtiow"
version = "0.1.0"
edition = "2021"

[dependencies]
image = "0.23"
nalgebra-glm = "0.15"
palette = { git = "https://github.com/Ogeon/palette.git" }
indicatif = "0.16"
rand = "0.8"
rayon = "1.5.1"
MDAP Hacky Hour

Modern tooling

Rust analyzer

  • Type hints
  • Refactoring
  • Diagnostics
  • Documentation

MDAP Hacky Hour

Change in development cycle

C, C++, Fortran, etc.

  • Compile → fix errors → compile → fix errors → [...]
  • run → seg fault → spend days trying to find overflow bug → crack out gdb + valgrind
  • test → fix silent bugs
  • test → fix logic bugs
  • production!
MDAP Hacky Hour

Change in development cycle

Rust

  • Fix errors and warnings as you write your code
  • test -> fix logic bugs
  • production!
MDAP Hacky Hour
MDAP Hacky Hour

https://msrc-blog.microsoft.com/2019/07/22/why-rust-for-safe-systems-programming/