Rust

Fast, safe and productive — pick three

An introduction to the Softwerkskammer Leipzig (2017-11-23)

Common system programming problems:

  • Undefined behaviors
  • Iterator invalidation
  • Data races

Undefined behaviors


int arr[4] = {0, 1, 2, 3};
int *p = arr + 5; // undefined behavior
                        

int k, i;
for (i = 0; i < 10; i++) {
    k = k + 1; // What is k?
}
                        

Iterator invalidation


vector<int> x = { 1, 2, 3 };
int j = 0;
for (auto it = x.begin(); it != x.end(); ++it) {
    x.push_back(j); // Makes iterator invalid.
    j++;
    cout << j << " .. ";
}
                        
Iterators are invalidated by some operations that modify a std::vector.

Data races


if (x == 5) { // The "Check"

   y = x * 2; // The "Act"

   // If another thread changed x
   // in between "if (x == 5)" and
   // "y = x * 2" above, y will not be 
   // equal to 10.
}
                        

Why is this a problem?

  • Runtime failures like segfaults are not recoverable.
  • Extremely common source of security bugs.
  • Existing solutions for safety are slow and usually require a garbage collection.
  • Non type safety leads into common pitfalls (void pointers, wrong casts).
  • Code Review for such corner cases slows down the development.

Features

  • General thread safety due to borrow checker
  • Powerful zero cost abstractions without garbage collection
  • Full featured Error handling
  • Lifetime and Ownership (memory safety)
  • Pattern matching
  • Traits for behavioral description
  • Modern library documentation
  • Highly optimized iterators
  • Closures
  • Futures
  • I/O Streams
  • Powerful macros
  • efficient C bindings
  • Program Structure, Modules, Crates
  • External crates

Demo

Resources

We have a blog.

Two good books, one free:

Thank you!