The Rust Programming Language

Nate Mara

2016-03-30

Intro

Rust is a systems programming language that combines the speed of C/C++ with the 'safety' of garbage-collected languages like Python & Java. It is being developed by Mozilla for their new browser engine, Servo.

Why Rust?

Rust has several features that make it a good choice for a project:

  • Zero-cost abstractions
  • Guaranteed memory safety at compile time
  • Trait-based generics
  • Type inference
  • Pattern matching

Zero Cost Abstractions

Python Example

Guaranteed Memory Safety

This has a few important implications:

  • No Segmentation Faults!
  • No memory leaks!
  • No data races in multithreaded programs!
  • No Null Pointers!!!!!

Trait Based Generics

Consider this C++ code:

template <class T>
bool foo(T x, T y) {
    return x == y;
}

Equivalent Rust:

fn foo<T: PartialEq>(x: T, y: T) -> bool {
    x == y;
}

Immutability By Default

  • Variables are not mutable by default
  • Must explicitly request mutability
let x = 100;
x += 10; // compiler error
let mut x = 100;
x += 10;

Type Inference

The type of variables does not explicitly have to be defined when defining variables.

let mut hashes = HashSet::new();
hashes.insert("00f013675328fac432");

Algebraic Data Types

  • Create new types based on combinations of other types
  • Not the same as creating classes with getters and setters
  • This is a type that can only have a specific number of values
enum Suit {
    Hearts,
    Clubs,
    Diamonds,
    Spades,
}

Patten Matching

  • Borrowed from Haskell
  • Can match against boolean conditions or variants of an enum.
  • More powerful than a switch statement as it forces you to account for all possibilities.
match card.suit {
    Hearts => println!("Hearts"),
    Clubs => println!("Clubs"),
    Spades => println!("Spades"),
} // will not compile because Diamonds case was not dealt with

No Null Pointers!

  • No way to create a null pointer in normal Rust
  • Only used when interfacing with C

    • Have to explicitly check for null pointer
match deck.draw_card() {
    Some(card) => println!("You picked {}", card),
    None => println!("No more cards left!"),
}

Ownership

Five rules of ownership

  1. You control when that resource is deallocated.
  2. You may lend that resource, immutably, to as many borrowers as you'd like.
  3. You may lend that resource, mutably, to a single borrower. BUT
  4. Once you've done so, you may not also lend it out otherwise, mutably or immutably.
  5. You may not lend it out mutably if you're currently lending it to someone.