closures for rust
play

Closures for Rust Michael Sullivan August 18, 2011 1 / 25 - PowerPoint PPT Presentation

Introduction Rust Closures Conclusion Closures for Rust Michael Sullivan August 18, 2011 1 / 25 Introduction Rust Closures Conclusion Outline Introduction Rust Closures 2 / 25 Introduction Rust Closures Conclusion


  1. Introduction Rust Closures Conclusion Closures for Rust Michael Sullivan August 18, 2011 1 / 25

  2. Introduction Rust Closures Conclusion Outline Introduction Rust Closures 2 / 25

  3. ❼ ❼ ❼ Introduction Rust Closures Conclusion Disclaimer 3 / 25

  4. ❼ ❼ Introduction Rust Closures Conclusion Disclaimer ❼ Rust is under heavy development. 3 / 25

  5. ❼ Introduction Rust Closures Conclusion Disclaimer ❼ Rust is under heavy development. ❼ The things described in this talk may not be true tomorrow. 3 / 25

  6. Introduction Rust Closures Conclusion Disclaimer ❼ Rust is under heavy development. ❼ The things described in this talk may not be true tomorrow. ❼ What I discuss and how I present issues reflect my personal biases in language design. 3 / 25

  7. ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Goals What do we want in a programming language? 4 / 25

  8. ❼ ❼ ❼ Introduction Rust Closures Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code 4 / 25

  9. ❼ ❼ Introduction Rust Closures Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent certain bugs 4 / 25

  10. ❼ Introduction Rust Closures Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent certain bugs ❼ Concurrent: easy to build concurrent programs and to take advantage of parallelism 4 / 25

  11. Introduction Rust Closures Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code ❼ Safe: type system provides guarantees that prevent certain bugs ❼ Concurrent: easy to build concurrent programs and to take advantage of parallelism ❼ “Systemsy”: fine grained control, predictable performance characteristics 4 / 25

  12. ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy 5 / 25

  13. ❼ ❼ ❼ Introduction Rust Closures Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe 5 / 25

  14. ❼ ❼ Introduction Rust Closures Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent 5 / 25

  15. ❼ Introduction Rust Closures Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent ❼ Haskell is (sometimes) fast, (very) safe, and concurrent 5 / 25

  16. Introduction Rust Closures Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy ❼ ML is (sometimes) fast and (very) safe ❼ Erlang is safe and concurrent ❼ Haskell is (sometimes) fast, (very) safe, and concurrent ❼ Java and C# are fast and safe 5 / 25

  17. Introduction Rust Closures Conclusion Rust a systems language pursuing the trifecta safe, concurrent, fast -lkuper 6 / 25

  18. Introduction Rust Closures Conclusion Rust Design Status 7 / 25

  19. ❼ ❼ ❼ ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) 8 / 25

  20. ❼ ❼ ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention 8 / 25

  21. ❼ ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment 8 / 25

  22. ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances 8 / 25

  23. ❼ ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ Clarity and precision of expression 8 / 25

  24. ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ Clarity and precision of expression ❼ Implementation simplicity 8 / 25

  25. ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ Clarity and precision of expression ❼ Implementation simplicity ❼ Run-time efficiency 8 / 25

  26. Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment ❼ System building, analysis and maintenance affordances ❼ Clarity and precision of expression ❼ Implementation simplicity ❼ Run-time efficiency ❼ High concurrency 8 / 25

  27. Introduction Rust Closures Conclusion Design Type system features ❼ Algebraic data type and pattern matching (no null pointers!) ❼ Polymorphism: functions and types can have generic type parameters ❼ Type inference on local variables ❼ Lightweight object system ❼ Data structures are immutable by default 9 / 25

  28. Introduction Rust Closures Conclusion Design Other features ❼ Lightweight tasks with no shared state ❼ Control over memory allocation ❼ Move semantics, unique pointers ❼ Function arguments can be passed by alias ❼ Typestate system tracks predicates that hold at points in the program 10 / 25

  29. Introduction Rust Closures Conclusion Design ...What? “It’s like C++ grew up, went to grad school, started dating ML, and is sharing an office with Erlang.” 11 / 25

  30. ❼ ❼ ❼ Introduction Rust Closures Conclusion Status rustc ❼ Self-hosting rust compiler 12 / 25

  31. ❼ ❼ Introduction Rust Closures Conclusion Status rustc ❼ Self-hosting rust compiler ❼ Uses LLVM as a backend 12 / 25

  32. ❼ Introduction Rust Closures Conclusion Status rustc ❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼ Handles polymorphism through type passing (blech) 12 / 25

  33. Introduction Rust Closures Conclusion Status rustc ❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼ Handles polymorphism through type passing (blech) ❼ Memory management through automatic reference counting (eww) 12 / 25

  34. ❼ ❼ Introduction Rust Closures Conclusion Status The catch ❼ Not ready for prime time 13 / 25

  35. ❼ Introduction Rust Closures Conclusion Status The catch ❼ Not ready for prime time ❼ Lots of bugs and exposed sharp edges 13 / 25

  36. Introduction Rust Closures Conclusion Status The catch ❼ Not ready for prime time ❼ Lots of bugs and exposed sharp edges ❼ Language still changing rapidly 13 / 25

  37. Introduction Rust Closures Conclusion Closures What closures are Closures in rust 14 / 25

  38. Introduction Rust Closures Conclusion What closures are Definition ❼ In civilized languages, functions are first-class values and are allowed to reference variables in enclosing scopes ❼ That is, they close over their environments 15 / 25

  39. Introduction Rust Closures Conclusion What closures are Example function add(x) { return function(y) { return x + y; }; } var foo = add (42)(1337); // 1379 ❼ Produces a function that adds x to its argument ❼ Note that the inner function outlives the enclosing function. x can’t just be stored on the stack. 16 / 25

  40. Introduction Rust Closures Conclusion What closures are Another Example function scale(x, v) { return map(function(y) { return x * y; }, v); } var v = scale(2, [1, 2, 3]); // [2, 4, 6] ❼ Multiplies every element in an array by some amount ❼ Note that here the lifetime of the inner function is shorter than the lifetime of the enclosing one. x could just be stored on the stack. 17 / 25

  41. ❼ Introduction Rust Closures Conclusion What closures are Traditional implementation ❼ Represent functions as a code pointer, environment pointer pair 18 / 25

  42. Introduction Rust Closures Conclusion What closures are Traditional implementation ❼ Represent functions as a code pointer, environment pointer pair ❼ Heap allocate stack frames (or at least the parts that are closed over) 18 / 25

  43. Introduction Rust Closures Conclusion Closures in rust Design constraints ❼ Want to be explicit about when we are allocating memory ❼ Don’t want to have to heap allocate closures when it isn’t necessary 19 / 25

  44. ❼ ❼ ❼ Introduction Rust Closures Conclusion Closures in rust Solutions ❼ Have two function types: block and fn 20 / 25

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend