Rust's secret sauce

60b23f3de35058180807f35b623fcf83?s=47 コン
November 10, 2019

Rust's secret sauce

60b23f3de35058180807f35b623fcf83?s=128

コン

November 10, 2019
Tweet

Transcript

  1. RUS’T SECRET SAUCE WHAT IS RUST DOING AND WHY IS

    IT COOL FOR THE FUNCTIONALLY ORIENTED?
  2. ςΩετ WHO AM I? ▸ Name: Pyry Kontio (ίϯςΟΦ) ▸

    Twitter: @GolDDranks ▸ From: Finland ▸ Does: A software engineer in a Japanese company ▸ Also does: Rust related stuff and programming for fun! ▸ A Rustacean for a little over 5 years – before 1.0!
  3. ςΩετ WHO AM I? ▸ as of late: ▸ writing

    streaming
 parsers in Rust ▸ as a result, trying
 to contribute some
 good patterns back
  4. ςΩετ WHO AM I? ▸ not everything goes
 as planned!

  5. TODAY’S AGENDA 1. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? 2.

    WHAT IS RUST DOING AND WHY? (3. INTERESTING DEVELOPMENTS)
  6. WHAT THE HECK IS FUNCTIONAL PROGRAMMING?! THIS IS A FUNCTIONAL

    PROGRAMMING CONFERENCE SO Y’ALL SUPPOSED TO KNOW, BUT BEAR WITH ME…
  7. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? THE STORY OF TWO

    DEFINITIONS ▸ the narrow one: the story of λ ▸ 1. functions are values too! ▸ 2. side-effects? let’s not. ▸ the wider one: the story of types, immutability, messages, linearity, effects.. ▸ in other words: here's the premise... ▸ but what should we build on top of it?
  8. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? THE LEGACY OF LAMBDA

    CALCULUS ▸ the mathematical "bias": there are things we never dispute ▸ evaluating an expression doesn't change anything ▸ referential transparency ▸ once defined object doesn't mutate ▸ immutability ▸ cloning information doesn't cost anything ▸ so obvious that only its converse has got a name: ▸ linearity
  9. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? THE LEGACY OF LAMBDA

    CALCULUS ▸ the mathematical "bias": there are things we never dispute ▸ anything is a first-class object ▸ i.e. we don't have to care about implementation details for practicality or efficiency ▸ including: functions, sets (= types), sets of functions (= lambdas/closures/function pointers), functions with codomain in sets (= type constructors)... but also including relations, proofs, signatures, modules (in all of its meanings), algebras...
  10. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? WHEN THINGS GET REAL:

    LISP ▸ it's simple and elegant ▸ it's quick and dirty ▸ ...depending on the implementation.
  11. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? WHEN THINGS GET SOPHISTICATED:

    ML ▸ what happened? ▸ we just embraced a whole lot of: ▸ complexity ▸ sophistication ▸ robustness
  12. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? WHEN THINGS GET SERIOUS:

    HASKELL ▸ let's take the lessons of ml ▸ and climb back to the ivory tower ▸ total purity ▸ laziness ▸ higher order anything
  13. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? WHY IS THIS GREAT?

    OR EVEN DECENT? ▸ iron-clad properties: ▸ no side-effects ▸ subdelegation with trust ▸ functions calling functions ▸ knowing that things you call can't hurt you ▸ functions as values ▸ expressivity: code is data — data is code
  14. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? WHY IS THIS GREAT?

    OR EVEN DECENT? ▸ iron-clad properties: ▸ lazyness ▸ expressivity! write what you think, not what is efficient ▸ types ▸ the compiler is going to check your bugs for you ▸ and in case you didn't have any types, infer them
  15. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? BUT WHAT ABOUT! ▸

    simula ▸ begat: smalltalk ▸ begat?: erlang ▸ begat: elixir (← we are finally talking about modern things, huh?) ▸ these languages are about objects?!
  16. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? BUT WHAT ABOUT! ▸

    begat?: erlang ▸ begat: elixir (← we are finally talking about modern things, huh?) ▸ these languages are about objects?! ▸ but they are principled! ▸ maybe we are on to something?!
  17. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? SMALLTALK: OBJECTS HAVING A

    CHAT ▸ a minimal language of
 a bunch of objects talking to each other ▸ no object can violate the state of another object ▸ encapsulation ▸ message passing
  18. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? ERLANG: WHAT IF WE

    DID ALL OF THIS? ▸ embracing the core tenets: ▸ functions are values ▸ immutability ▸ still, the world is made of mutable state ▸ communicating objects ▸ control by message passing
  19. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? SO, WHAT THE HECK

    IS FUNCTIONAL PROGRAMMING? ▸ the simpl(istic) answer: a haskell (← not to be taken lightly!) ▸ the wider view: ▸ anything that is inspired/motivated by the the core ideas ▸ including things that aren't strictly limited to programming with functions ▸ "functional" is programming with smart, good ideas
  20. WHAT THE HECK IS FUNCTIONAL PROGRAMMING? SO, WHAT THE HECK

    IS FUNCTIONAL PROGRAMMING? ▸ furthermore, functional programming
 is not a property of a a language ▸ even though some languages have affinity to FP ▸ it's a style of writing programs
  21. WHAT IS RUST DOING? AND WHY? ALL RIGHT, THIS IS

    THE REASON WE ARE HAVING THIS CONVERSATION.
  22. WHAT THE HECK IS SYSTEMS PROGRAMMING? ALL RIGHT, THIS IS

    THE REASON WE ARE HAVING THIS CONVERSATION.
  23. WHAT IS RUST DOING? AND WHY? SYSTEMS PROGRAMMING ▸ what

    is it for ▸ what is it like
  24. WHAT IS RUST DOING? AND WHY? SYSTEMS PROGRAMMING - WHAT

    IS IT FOR? ▸ providing a platform for application programming ▸ required: low level control ▸ providing shared functionality ▸ libraries ▸ sometimes required:
 being agnostic of a platform
  25. WHAT IS RUST DOING? AND WHY? SYSTEMS PROGRAMMING - WHAT

    IT IS LIKE? ▸ constraints! ▸ resource constraints ▸ memory constraints ▸ time constraints ▸ max. hardware
 utilization ▸ being serious about control
  26. WHAT IS RUST DOING? AND WHY? CHALLENGING OUR "MATHEMATICAL BIAS"

    ▸ many of our assumptions don't
 survive a contact with the real world ▸ not all information is easily clonable ▸ resources: files, network connections, hardware ▸ the real world is mutable ▸ we can't afford sacrificing performance and control
 for nicer abstractions
  27. WHAT IS RUST DOING? AND WHY? CHALLENGING OUR "MATHEMATICAL BIAS"

    ▸ → functional programming is awesome but it doesn't match how the world works too well ▸ in systems programming, you must care
 about how to world works!
  28. WHAT IS RUST DOING? AND WHY? A CHASM BETWEEN SYSTEMS

    AND FUNCTIONAL PROGRAMMING systems programming functional programming
  29. WHAT IS RUST DOING? AND WHY? HOW RUST CHANGES THE

    PICTURE systems
 programming functional
 programming what rust does interesting to us!
  30. OK, WHAT ABOUT RUST?

  31. WHAT IS RUST DOING? AND WHY? SYSTEMS PROGRAMMING LANGUAGES? ▸

    C ▸ C++ ▸ Ada ▸ D ▸ Rust ← the only one here that can be said to do something remotely functional! ▸ (I wish to see on this list someday: Zig)
  32. WHAT IS RUST DOING? AND WHY? RUST IS SOMETHING NEW

    ▸ unashamedly systems-y programming language ▸ more C++ than C++,
 in where C++ means the good parts of C++ ▸ uncompromisingly memory safe ▸ the division between "safe" and "unsafe"parts
 actually means something ▸ uncannily functional → we want to delve into this!
  33. WHAT ABOUT RUST IS FAMILIAR FOR FP PEOPLE?

  34. WHAT IS RUST DOING? AND WHY? FAMILIAR: STATE DON'T CHANGE

    UNDER YOUR FEET ▸ the compiler is going to be angry at you if you try! ▸ let state = State!::new();
 state.method(); ▸ we can be sure that state is still the same
 after the method call! ▸ there are exceptions though...
  35. WHAT IS RUST DOING? AND WHY? FAMILIAR: CLOSURES/LAMBDAS ▸ functions

    are first-class values ▸ let people = "Wizard Whitebeard, Woof, Odlaw, Wenda, and Wally";
 
 let initials = people.find(
 |c: char| c.is_uppercase());
 ↑ anonymous function
  36. WHAT IS RUST DOING? AND WHY? FAMILIAR: CLOSURES/LAMBDAS ▸ example:

    nom, the parser combinator library
  37. WHAT IS RUST DOING? AND WHY? FAMILIAR: ALGEBRAIC DATA TYPES

    ▸ structs/tuples/product types, of course ▸ but also: sum types/tagged unions/variants: ▸ enum ParserState {
 NeedsMoreInput,
 HasMoreOutput {
 unparsed_bytes: &[u8],
 output: Vec<Objects>,
 },
 Error(ParseError),
 }
  38. WHAT IS RUST DOING? AND WHY? FAMILIAR: LAZINESS, ITERATORS, ADAPTERS

    ▸ you can iterate over collections, map, flat_map, filter etc. ▸ let a = [0, 1, 2];
 let mut iter = a.iter().filter(|&x| *x > 1);
 iter.next() ▸ produces Some(2)
 

  39. WHAT IS RUST DOING? AND WHY? FAMILIAR: TYPE CLASSES AND

    PARAMETRIC POLYMORPHISM ▸ abstracting functionality that is common between types ▸ trait Eq {
 fn eq(&self, other: Self) !-> bool;
 } ▸ sadly, no higher kinded types supported at the moment...
 

  40. WHAT ABOUT RUST IS NEW FOR FP PEOPLE?

  41. WHAT IS RUST DOING? AND WHY? FRESH IDEAS: LINEAR/AFFINE TYPES

    & OWNERSHIP ▸ a value that can have only one owner ▸ you can track resources precisely ▸ you have the full control
 because only you have the access ▸ when you're done, you know when to destroy it
  42. WHAT IS RUST DOING? AND WHY? FRESH IDEAS: LINEAR/AFFINE TYPES

    & OWNERSHIP IT'S MY BOOK, I CAN DO WHATEVER I WANT WITH IT!
  43. WHAT IS RUST DOING? AND WHY? FRESH IDEAS: LINEAR/AFFINE TYPES

    & OWNERSHIP INCLUDING GIVING IT TO YOU. WOW, THANKS!
  44. WHAT IS RUST DOING? AND WHY? FRESH IDEAS: LINEAR/AFFINE TYPES

    & OWNERSHIP I AM GOING TO THROW IT AWAY. ...
  45. WHAT IS RUST DOING? AND WHY? FRESH IDEAS: LINEAR/AFFINE TYPES

    & OWNERSHIP IT'S GARBAGE CAN, NOT GARBAGE CAN'T! I GUESS I HAVE NO SAY IN THAT. YOU'RE THE OWNER NOW.
  46. WHAT IS RUST DOING? AND WHY? FRESH IDEAS: LINEAR/AFFINE TYPES

    & OWNERSHIP ▸ fn read_book(book: Book) {
 println!("Reading {}", book);
 }
 
 let b = Book!::new();
 read_book(b); ← Move the value inside the function
 read_book(b); ← COMPILER ERROR: CAN'T USE A MOVED VALUE!
  47. WHAT IS RUST DOING? AND WHY? FRESH IDEAS: LINEAR/AFFINE TYPES

    & OWNERSHIP ▸ mutating stuff we own is OK. ▸ from FP perspective it's the same as consuming a value and creating a new one! ▸ fn read_book(mut book: Book) {
 book.write_notes(); ← mutation!
 println!("Reading {}", book);
 }
 
 let b = Book!::new();
 read_book(b);
  48. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS I'LL LEND MY BOOK TO YOU. NICE.
  49. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS THINK WHAT YOU LIKE, BUT THAT IS MY BOOK. YOU ARE NOT GOING TO THROW IT AWAY. I THINK IT'S GARBAGE
  50. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS FINISHED WITH IT? I'LL HAVE IT BACK, THANK YOU VERY MUCH. ...
  51. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS ▸ Borrowing takes a reference to an object. The borrow automatically ends, and the original owner has it back. ▸ fn read_book(book: &Book) {
 println!("Reading {}", book);
 }
 
 let b = Book!::new();
 read_book(&b); ← The function borrows the value
  52. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS ▸ there is two kinds of borrowing: ▸ shared: &book ▸ allows multiple simultaneous borrows ▸ doesn't normally allow mutating the borrowed thing ▸ unique: &mut book ▸ allows only one simultaneous borrow ▸ allows mutating the borrowed thing
  53. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS ▸ borrowing stuff as shared gives you the peace of mind ▸ i'm passing a reference to my book to this function, but it can't mutate or deallocate it ▸ let b = Book!::new();
 let borrow_1 = &b; ← storing the reference in a variable
 let borrow_2 = &b; ← creating a second one is OK!
 compare_books(borrow_1, borrow_2);
  54. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS I'LL LEND MY BOOK TO YOU. YOU CAN SCRIBBLE IN IT. NEAT.
  55. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS WHATEVER. I'M GONNA WRITE SOMETHING NASTY IN IT.
  56. WHAT IS RUST DOING? AND WHY? FOR ERGONOMCS & PERFORMANCE:

    BORROWS ▸ borrowing stuff as unique is powerful! ▸ the borrower can mutate the thing! ▸ but importantly, they can't deallocate it. ▸ unique means here that even the owner
 can't access it while it's borrowed! ▸ let mut b = Book!::new();
 scribble_in_book(&mut b);
  57. RUST'S SECRET SAUCE IT'S TIME TO SPILL THE RECIPE.

  58. WHAT IS RUST DOING? AND WHY? IN A NUTSHELL: RUST'S

    SECRET SAUCE ▸ MUTABILITY XOR SHARING ▸ ...is enough to preserve ▸ your code's safety ▸ your sanity ▸ functional programming that limits itself to immutable, shareable values is leaving the other option to the table!
  59. WHAT IS RUST DOING? AND WHY? MUTABILITY XOR SHARING ▸

    additionally: affine typing gets a special mention ▸ stretching the metaphor: 
 it's the most important spice in the sauce. ▸ affine means that the type system ensures that
 a value is used at most once. ▸ without the ability to ensure that,
 we couldn't control sharing
  60. WHAT IS RUST DOING? AND WHY? MUTABILITY XOR SHARING ▸

    mutability makes rust suitable for system programming ▸ but the ability to restrict it makes Rust sane – in a way that resembles functional programming!
  61. WHAT IS RUST DOING? AND WHY? IS RUST A FUNCTIONAL

    PROGRAMMING LANGUAGE? ▸ in a narrow sense, it isn't ▸ but i like to think it as a part of the bigger story
 of history of functional programming ▸ Rust takes inspiration from a huge number of great ideas from functional language ▸ i sincerelly hope it also is able to give something back
  62. INTERESTING DEVELOPMENTS THE EXCITEMENT NEVER ENDS.

  63. INTERESTING DEVELOPMENTS INCOMING FEATURES IN RUST: CONST FN ▸ going

    after referential transparency! ▸ const functions ▸ functions that are executable compile-time ▸ functions that only, and deterministically
 depend on the inputs
  64. INTERESTING DEVELOPMENTS INCOMING FEATURES IN RUST: CONST FN ▸ is

    this referentially transparent? ▸ fn mutate_thing(&mut Thing) ▸ traditionally, no! ▸ but because of the sharing restriction,
 it can be regarded as that: ▸ state before call ≒ input ▸ state after call ≒ output
  65. INTERESTING DEVELOPMENTS INCOMING FEATURES IN RUST: GAT ▸ more powerful

    types: "generic associated types" ▸ needed mostly because there are some patterns
 around "streaming iterators" and "collections"
 that the type system is unable to represent ▸ however, brings some power of
 higher-kinded types to the language
  66. INTERESTING DEVELOPMENTS INCOMING FEATURES IN RUST: GAT ▸ more powerful

    types: "generic associated types" ▸ trait Collection<T> {
 fn empty() !-> Self;
 fn add(&mut self, value: T);
 
 fn iterate<'iter>(&'iter self) !->
 Self!::Iter<'iter>;
 type Iter<'iter>: Iterator<Item=T>;
 } ▸
  67. INTERESTING DEVELOPMENTS INCOMING FEATURES IN RUST: SPECIALIZATION ▸ specializing parametrically

    polymorphic
 bounds based on the exact type
 (like C++ template specialization) ▸ this might be sad for some haskellers: ▸ partial loss of "parametricity": less "theorems for free" ▸ however, it's an excellent example of an balancing act between systems and functional programming
  68. INTERESTING DEVELOPMENTS FINALLY: THE RUST COMMUNITY AND ECOSYSTEM ▸ growing

    at rapid speed ▸ very welcoming for newcomers ▸ we welcome you to take part and explore Rust!
  69. INTERESTING DEVELOPMENTS FINALLY: THE RUST COMMUNITY AND ECOSYSTEM ▸ the

    most loved language fours years in a row (2016-2019)!
  70. INTERESTING DEVELOPMENTS FINALLY: THE RUST COMMUNITY AND ECOSYSTEM ▸ for

    the Japanese community: ▸ rust-jp slackͰޠΓ߹͍·͠ΐ͏ʂ ▸ https://rust-jp.herokuapp.com/ ▸ ఆظతʹLTձ΍ษڧձ΍Γ·͢ʂ
 ࠷ۙɺ೔ຊॳͷRustΧϯϑΝϨϯε։࠵ʂ ▸ https://rust.tokyo ▸ https://rust.connpass.com
  71. THANK YOU FOR LISTENING. RUST COMMUNITY WELCOMES YOU! @GolDDranks