Intro to Rust

What is Rust? Where is Rust? Why is Rust?

Created by Joshua Nelson with reveal.js

Where did Rust come from?

In many ways, a reaction to other languages

no ideas newer than 10 years old

Memory safety

  • Null pointers
  • Out-of-bounds accesses
  • Use after free (including iterator invalidation)
  • Integer overflow

See my previous talk for details

Thread safety

In C especially, most libraries not designed with threads in mind

Example: errno is very broken in the presence of threads (__errno_location)

It turns out all my slides are actually just Amos blog posts repackaged

Backwards compatibility

Or, why is unique_ptr slow?

  • C++ has the worst of both worlds: unstable ABI that can't be changedWhat is an ABI?
  • Three languages in one
  • Python: The standard library is where modules go to die

What is Rust?

  • Reliable
  • Efficient
  • Polished

Reliable

  • Type-safety with Option and Result
  • Memory and thread safety with unsafe and borrow-checking
  • Extremes: Type state

Rust users want static verification
image credit: leftoversalad comics

Type-safety

Option is a library features, not a language feature

Combinators!

let other: Vec<String> = response
  .get("warnings")
  .and_then(|j| j.get("other"))
  .and_then(|j| j.as_array())
  .map(|x| x.iter().flat_map(|j| j.as_str()).map(Into::into).collect())
  .unwrap_or_else(Vec::new);

^ real code taken from cargo

Rust was created by a secret cabal of Haskell developers to teach programmers monads

Type-safety

Result means errors can't be ignored

Let's pick on Go
(taken from official error handling page): playground

f, err := os.Open("filename.ext")
if err != nil {
    log.Fatal(err)
}
// do something with the open *File f

Not unique to Go - C, C++, most HTTP libraries have the same issue

Vaguely adapted from Abstracting Away Correctness

Memory Safe

  • "Safe" has a very specific meaning: memory safety and thread safety
  • See previous talk for details
  • Speed of C/C++ without segfaults
  • Borrow checker: shared XOR mutable
  • unsafe gives you an escape hatch

Efficiency

Compile speed vs. runtime

Polished

  • Standard build tool, Cargo, that doesn't suck (looking at you CMake, pip)
  • 1st-class documentation (Rustdoc, docs.rs)
  • Standard formatter (Rustfmt)
    • disclaimer, I hate rustfmt, it's the worst formatter except for all the others
  • Orthogonal language features (orthogonal → independent, complement each other)
    • Traits and operator overloading
    • cfg and unit tests

Who is using Rust?

Lots of people!

  • Amazon
  • Microsoft
  • Firefox
  • Dropbox
  • 1Password
  • npm
  • System76
  • ... more

The real reason to use rust

The real reason to use rust

Ferris emojis

Where can I learn more?