Intro to Rust

Safe, correct, and portable solutions.

Presenter Info

Software Engineer working at American Express. Interested in scientific computing, natural language processing, and Rust.

What is Rust?

“A language empowering everyone to build reliable and efficient software.”

What kind of language is it?

  • Ahead-of-time compiled
  • Strong, affine types
  • Minimal runtime, no garbage collector
  • No runtime exceptions
  • Functional programming, if you want it

What kind of language is it?

  • Ahead-of-time compiled
  • Strong, affine types
  • Minimal runtime, no garbage collector
  • No runtime exceptions
  • Functional programming, if you want it
  • Some* object orientation, if you want it

Advantages

  • Correctness
  • Fearless concurrency
  • Memory safety
  • Portability

Who is using Rust?

See more: Friends of Rust

Consider Rust for problems with these properties:

  • Highly parallel
  • High availability requirements
  • High performance requirements
  • Low-level memory manipulation
  • Hardware binary portability
  • Language interoperability

Example - Visualizing the Mandelbrot set:

  • Highly parallel
  • High performance requirements
  • Language interoperability

Example Implementation

The Mandelbrot Set

Mandelbrot Iteration


					use num_complex::Complex;

					fn abs(c: Complex<f64>) -> f64 {
					    (c * c.conj()).re
					}
					

Mandelbrot Iteration


					use num_complex::Complex;

					fn abs(c: Complex<f64>) -> f64 {
					    (c * c.conj()).re
					}

					pub fn mandelbrot(
					    c: Complex<f64>,
					    threshold: f64,
					    max_steps: u8
					) -> u8 {
					    let mut z = c.clone();
					    let mut i = 1u8;
					    while i < max_steps && abs(z) < threshold {
					        z = z * z + c;
					        i += 1;
					    }

					    // see: "Renormalizing the Mandelbrot Escape"
					    // https://linas.org/art-gallery/escape/escape.html
					    if i == max_steps {
				            max_steps
					    } else {
						    i + 1 - abs(z).log2().log10().round() as u8
					    }
					}
					

Rust libraries are Crates

Open source crates are published to crates.io

mandelbrot_common
crates.io/crates/mandelbrot_common

Source:
github.com/ahoetker/mandelbrot

Python Package

Rust can be used to create Python packages.

mandelbrot_py
pypi.org/project/mandelbrot_py/

Source:
github.com/ahoetker/mandelbrot-rs

Demo notebook:
mybinder.org

Thread-safe concurrency without the GIL

WebAssembly Module

Rust has excellent tooling to target WebAssembly.

In fact, the next slide demonstrates a wasm module compiled using the same Mandelbrot crate.

Source:
github.com/ahoetker/mandelbrot-wasm

Questions?