Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GoTo nights: Modern low-level - Rust

GoTo nights: Modern low-level - Rust

Florian Gilcher

October 05, 2017
Tweet

More Decks by Florian Gilcher

Other Decks in Programming

Transcript

  1. r Backend Developer r Rust Trainer r Ruby Programmer since

    2003 r Rust Programmer since 2013 r CEO asquera GmbH
  2. r Community person r Rust UG Berlin/Karlsruhe r Search UG

    Berlin r Ex-chair of Ruby Berlin e.V.
  3. r new systems programming language r powers and was developed

    in along with Servo, a new browser engine r by Mozilla
  4. r Static type system with local type inference r Explicit

    mutability r Zero-cost abstractions r Runtime-independent concurrency primitives
  5. r Explicit error control flow r No null r Static

    automatic memory manage- ment r No garbage collection
  6. r Built with practical use in mind from the beginning

    r Features are there for the long run r Still with an eye on ergonomics r Great tooling and docs
  7. extern crate tempdir; use tempdir::*; use std::fs::File; fn main() {

    let tempdir = TempDir::new("mozilla-roadshow"); let mut tempfile = match tempdir { Ok(dir) => { File::create( &dir.path().join("tmp") ) } Err(_) => { panic!("Couldn’t open tempdir") } } // look, no close necessary! }
  8. extern crate tempdir; use tempdir::*; use std::fs::File; fn main() {

    let mut tempfile = TempDir::new("mozilla-roadshow") .and_then( |tempdir| { File::create( &tempdir.path().join("tmpfile") ) }) ); //... }
  9. Base concept: Mutability struct InnerData { val: i32 } struct

    Data { inner: InnerData } fn main() { let d = Data { inner: InnerData { val: 41 }}; d.inner.val = 42; // error: cannot assign to immutable field `d.inner.val` }
  10. struct InnerData { val: i32 } struct Data { inner:

    InnerData } fn main() { let mut d = Data { inner: InnerData { val: 41 }}; d.inner.val = 42; }
  11. r Every piece of data is uniquely owned r Ownership

    can be passed r Or access can be borrowed (muta- ble and immutable)
  12. struct Data { value: String } fn main() { let

    data = Data { value: String::from("Hello Berlin!") }; taking_ownership(data); // data cannot be used here } fn taking_ownership(mut data: Data) { share_immutably(&data); share_mutably(&mut data); }
  13. r You can borrow mutably once r Or multiple times

    immutably r Exclusive: mutable or immutable, never both
  14. Mutability runs deep 100, 1000, 10.000 lines of called code,

    Rust keeps your mutability properties!
  15. use std::rc::Rc; fn increment(m: &mut i32) { *m += 1

    } fn main() { let ref_counted_integer = Rc::new(43); for i in 1..3 { let mut handle = ref_counted_integer.clone(); std::thread::spawn(move || { increment(&mut handle); // the trait `std::marker::Send` is not implemented for `std::rc::Rc<i32>` }); } }
  16. Rust has the concept of "sendable" and "syncable" values. The

    first can cross concurrency boundaries by being moved to the other side, the second allows sharing between.
  17. It also doesn’t impose an approach! Shared-nothing as well as

    sharing synced values can be expressed!
  18. Rust is not a simple language and many of its

    features are built for the long run. It takes a bit to click.
  19. Rust is approachable r Easy to get an environment setup

    r Great docs through the book and a (almost) fully documented standard API r Build and depencency management baked in
  20. Rust plays well with others r Rust interfaces with C

    or a C-FFI at almost no or zero cost r Binding generators C and C++ r High-level bindings for some lan- guages (Ruby, JavaScript)
  21. Rust is a great partner Rust preserves the conveniences of

    high-level languages in low-level land
  22. We don’t want to be your primary language at all

    cost, but be the best secondary one.
  23. Projects using Rust r GStreamer r Servo/Firefox r mononoke mercurial

    server r Redox OS r RTFM (Real Time For the Masses)
  24. r Rust has better ergonomics at same speed r The

    20 last security issues were memory bugs r Multithreading without support is a huge source of bugs
  25. r Rust gives latency guarantees GC’ed languages cannot give r

    Runs well on memory-constrained environments
  26. Commitment to stability r Rust is released every 6 weeks

    r Rust is backwards-compatible, with huge machinery to encure that r Currently at version 1.19.0
  27. Commitment to stability r Rust stable allows no use of

    unsta- ble features r These are only available in nightly builds
  28. Maturity r Code generation is provided through LLVM r No

    runtime: no bugs in the runtime r Very conservative approach to stdlib adoption
  29. Cross-capabilities r rustc is a cross-compiler by default and the

    whole toolchain is aware r Almost-no-setup cross compilation! (getting better) r Rust supports embedded usecases
  30. Great compiler support r rustc provides useful errors and warnings,

    including hints r currently in-development language server for supporting IDEs
  31. Many important libraries available: r Many protocols (HTTP, etc) r

    Parsers r Concurrency abstraction libraries
  32. Already there: r pre-built binary libs for 48 targets, ready

    to download r Full and advanced build tooling through cargo r webasm support (unstable)
  33. Governance & Community Rust is developed by an in-house team

    at Mozilla. That does not mean in is dependent on it.
  34. Supporting commercial users r regular interviews with prod users, to

    hear about their issues r the core team can always be spoken to r consulting, development and train- ing available, through integer32 and asquera.
  35. Rust is bringing safe programming to targets where it was

    unfeasible before while also bringing new things to the table to compete with other safe languages.
  36. r Rust Hack & Learn Berlin r 89 Meetups around

    the world r https://zurich.rustfest.eu