Introduction to Rust (December 2015)

Introduction to Rust (December 2015)

Rust is the new kid on the block. It runs on bare metal, is blazingly fast and offers sophisticated compile time checks. It offers memory safety without requiring a Garbage Collector.

Learning Rust will make you a better programmer.

I will give an introductory talk about Rust and show its nicest features. We'll also check out some interesting projects written in Rust.

Ed9e9992fe069a0d3de05b69d8d187c3?s=128

Matthias Endler

December 15, 2015
Tweet

Transcript

  1. Introduction to Rust

  2. Rust - Created by Graydon Hoare - Funded by Mozilla

    - Primary goal: A safer systems language - First version: 2010 - Current version 1.5 - Main project: Servo browser engine
  3. Systems Programming Memory management Error handling Static Typing Thread safety

    Compiling …
  4. Systems Programming is hard!

  5. • Provide the right abstractions • Provide awesome tools •

    Allow making mistakes • Build a helpful community Systems Programming can be fun!
  6. RUST: High level on bare metal

  7. None
  8. Where Rust makes sense • Operating Systems • Encryption •

    Databases • Graphics programming • … (everywhere you need speed and safety)
  9. Where Rust makes sense glium • High-level wrapper around OpenGL

    • Avoids all OpenGL errors diesel • A safe, extensible ORM and Query Builder • Operating System written in pure Rust,
 designed to be modular and secure Redox crossbeam • A collection of lock-less data structures turbine • A high-performance, non-locking, inter-task communication library written in Rust. • Go channels on steroids
  10. What I like about… • PHP: Package manager, Community •

    Python: Syntax, Libraries • Golang: Tooling, Documentation, Concurrency • C: Speed, no overhead
  11. What I like about… • PHP: Package manager, Community •

    Python: Syntax, Libraries • Golang: Tooling, Documentation, Concurrency • C: Speed, no overhead RUST
  12. What sucks about… • PHP: Syntax (a bit), legacy functions

    • Python: Package manager, 2 vs 3 • Golang: Error handling, package manager, no generics, Syntax (a bit) • C: Missing package manager, safety
  13. Complexity vs Speed • PHP • Python • Golang •

    Rust • C Faster „Easier“
  14. Popular systems languages Type safety Type expression Type checking Garbage

    Collector C unsafe explicit static No C++ unsafe explicit static No Go safe implicit/explicit static Yes Rust safe implicit/explicit static/dynamic optional
  15. #include <iostream> #include <vector> #include <string> int main() { std::vector<std::string>

    v; v.push_back("foo"); auto const &x = v[0]; v.push_back("bar"); // Undefined behavior! std::cout << x << '\n'; }
  16. What went wrong? Your vector is just an address in

    memory
  17. When it grows, it might be moved! What went wrong?

  18. Your old pointer still exists. What went wrong?

  19. …but it points to „something“ What went wrong?

  20. There’s multiple mutable references to the same data Observation

  21. There’s multiple mutable references to the same data Ownership Avoid

    There should only be one owner at the same time In Rust this is enforced by the compiler!
  22. Is this really an issue? Search results on StackOverflow.com: segfault

    10.899 results segmentation fault 21.238 results memory leak 55.064 results
  23. Ownership + Borrowing + Lifetimes = data safety Three important

    concepts in Rust
  24. Ownership in real life Me You Here’s a present! Book

    owner
  25. Ownership in real life Me You Thanks Dude! Book owner

  26. Ownership in real life Me You Book owner

  27. Borrowing in real life Me You Nice book! Can I

    lend it? Book owner
  28. Borrowing in real life Me You Thanks Dude! Sure! Book

    owner
  29. Borrowing in real life Me You Book owner

  30. Borrowing in real life Me You Thanks for lending Book

    owner
  31. fn f(x: Type) {...} fn f(x: &Type) {...} fn f(x:

    &mut Type) {...} Shared borrow Mutable borrow Ownership • one owner • mutable • readable • Share as you like • immutable • readable • one at a time • mutable • readable
  32. Show us some code already!

  33. What sucks about Rust • Fighting with the Borrow checker

    • Syntax (a bit) • Missing packages (you can help!) • probably more…
  34. Tool list Unit testing: builtin Package manager Syntax highlighting Formatting

    Code completion Debugging Code linter Benchmarking Profiling Code coverage
  35. https://doc.rust-lang.org/book/testing.html Unit testing

  36. Unit testing: builtin Package manager: cargo Syntax highlighting Formatting Code

    completion Debugging Code linter Benchmarking Profiling Code coverage Tool list
  37. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting

    Code completion Debugging Code linter Benchmarking Profiling Code coverage Tool list
  38. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting:

    rustfmt Code completion Debugging Code linter Benchmarking Profiling Code coverage Tool list
  39. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting:

    rustfmt Code completion: racer Debugging Code linter Benchmarking Profiling Code coverage Tool list
  40. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting:

    rustfmt Code completion: racer Debugging: rust gdb? Code linter Benchmarking Profiling Code coverage Tool list
  41. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting:

    rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking Profiling Code coverage Tool list
  42. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting:

    rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking: builtin Profiling Code coverage Tool list
  43. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting:

    rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking: builtin Profiling: meh… Code coverage Tool list
  44. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting:

    rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking: builtin Profiling: meh… torch? Code coverage Tool list
  45. https://llogiq.github.io/2015/07/15/profiling.html Calls Time

  46. Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting:

    rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking: builtin Profiling: meh… torch? Code coverage: kcov? Tool list
  47. kcov …is a code coverage tester for compiled programs, Python

    scripts and shell scripts language: rust matrix: fast_finish: true include: - rust: nightly env: FEATURES="--features nightly" sudo: false - rust: nightly env: FEATURES="--features nightly" BENCH=true sudo: false - rust: beta sudo: true after_success: | sudo apt-get install libcurl4-openssl-dev libelf-dev libdw-dev && wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz && tar xzf master.tar.gz && mkdir kcov-master/build && cd kcov-master/build && cmake .. && make && sudo make install && cd ../.. && kcov --coveralls-id=$TRAVIS_JOB_ID --exclude-pattern=/.cargo target/kcov target/debug/hyper-* Put this into your .travis.yml:
  48. Summary Lots of work to do But impressive start

  49. We need to focus on those… Unit testing Package manager

    Syntax highlighting Formatting Code completion Debugging Code linter Benchmarking Profiling Code coverage
  50. Bonus Rustlings (https://github.com/carols10cents/rustlings) Euler Rust (https://github.com/gifnksm/ProjectEulerRust) Rust libraries/packages (https://crates.io/)
 


    Rust patterns
 (https://github.com/nrc/patterns)
  51. Even more at https://gist.github.com/nrc/a3bbf6dd1b14ce57f18c