wezm.net/v1/output/technical/2018/01/introduction-to-rust-talk/slides/slides.md

9.8 KiB
Raw Permalink Blame History

class: center, middle, ferris

Introduction to Rust

Wesley Moore

Ferris the Rustacean animation


Agenda

  1. Features
  2. Tooling
  3. Concepts
  4. Demo
  5. Questions

Introduction

fn main() {
  println!("Rust Introduction");
}

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

rust-lang.org


Systems Programming Language

  • Compiles to native code.

--

  • Aims to solve the same sorts of problems that C and C++ are used to solve but with improved safety.

--

  • Also seeing use in:

--

  • Web (front and backend)

--

--

  • Embedded systems (microcontrollers)

Runs Blazingly Fast

Benchmarks Chart


Runs Blazingly Fast

  • No interpreter, no runtime.

--

  • Memory safety without garbage collection.

--

  • Zero cost abstractions.

class: segfaults

Prevents Segfaults

  • No nil, NULL or other [billion dollar mistakes][billion-dollar-mistake].

undefined method for nil:NilClass

This is not a thing. At all. Ever.

.center[[billion-dollar-mistake]: https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare]


Guarantees Thread Safety

  • Strongly, statically typed with an emphasis on safety and correctness.

--

  • Ownership model tracks owner and lifetime of memory.

--

  • No data races: Compiler knows which thread owns what data.

--

Mozilla made two previous attempts to parallelize its style system in C++, and both of them failed. But Rusts fearless concurrency has made parallelism practical!

Fearless Concurrency in Firefox Quantum (Nov 2017)


Strong Static Type System

  • Do more at compile time so fewer tests and runtime checks are required.

--

  • Concepts mostly familiar. No need to learn an entirely new paradigm.

--

  • Traits and generics instead of objects and inheritance.

--

  • Type inference reduces the need to provide type annotations.

--

  • Refactor with impunity — even in large code bases.

Tooling

Official distribution includes: cargo

:

  • Build tool (no Makefiles)

--

  • Package manager (like bundler)

--

  • Test runner

--

  • Documentation generator

Releases

Generally managed with rustup

:

  • Official toolchain manager (like rbenv)

--

  • New releases every 6 weeks
    • Committed to backwards compatibility in every release.

class: crates

Crates

  • Rust favours a small, stable standard library.
  • Crates are the equivalent of Ruby gems. They are published to crates.io.

screenshot of crates.io


Community and Direction

  • RFC process.

--

  • Systematic improvement.

--

  • Emphasis on inclusion and building a friendly community.

class: center, middle, bigger

Concepts


Functions

fn add(left: i32, right: i32) -> i32 {
    left + right
}

fn main() {
    let sum = add(2, 3);
}

Conditionals: if

let temperature: i32 = 29;

if temperature > 25 {
    println!("Wesley is happy");
}
else {
    println!("Too cold");
}

Conditionals: match

Can match structure and values:

let month = "jan";

match month {
    "jan" => 1,  "feb" => 2,
    "mar" => 3,  "apr" => 4,
    "may" => 5,  "jun" => 6,
    "jul" => 7,  "aug" => 8,
    "sep" => 9,  "oct" => 10,
    "nov" => 11, "dec" => 12,
    _     => panic!("invalid month"),
};


Loops

let numbers = [1, 2, 3];

for i in numbers.iter() {
    // do something
}

for i in 0..10 {
    // do something
}

Also less frequently used commonly used loop and while.


Functional or Imperative

fn variance_mean(data: &[f64], mean: f64) -> f64 {
    let mut sum = 0.;

    for d in data {
        sum += (d - mean).powf(2.0);
    }

    sum / data.len() as f64
}

Code from hydromath crate by Andrew MacDonald.


Functional or Imperative

fn variance_mean(data: &[f64], mean: f64) -> f64 {
    data.into_iter()
        .map(|d| (d - mean).powf(2.0))
        .sum::<f64>() / data.len() as f64
}

--

You pay no cost for using the higher level style, it compiles to identical machine code (I checked).


enums

Type that represents one possibility of several variants. Variants may optionally carry data.

enum SerialProtocol {
    Usb,
    Rs485,
    Rs232,
    I2C,
    Spi,
}

structs

Type that carries structured data.

struct Person {
    name: String,
    age: i32,
    favourite_serial_protocol: SerialProtocol,
}

impl Person {
    fn name_and_age(&self) -> String {
        format!("{} is {} years old", self.name, self.age)
    }
}

Option

Instead of nil and NULL there is Option.

  • Used to represent something that may be absent.
enum Option<T> {
    Some(T),
    None
}

Result

When something can succeed or fail with an error.

  • There are no exceptions in Rust, Result is how you handle errors.
enum Result<T, E> {
    Ok(T),
    Err(E)
}

Demo

Small tool that will determine a file's type from its extension:

filetype src/main.rs somefile.rb

Should give output like:

Rust: src/main.rs
Ruby: src/somefile.rb

class: center, middle, bigger

Demo


class: center, middle, invert, bigger

Questions?


Credits