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 Disclaimer 3 / 25
❼ ❼ Introduction Rust Closures Conclusion Disclaimer ❼ Rust is under heavy development. 3 / 25
❼ Introduction Rust Closures Conclusion Disclaimer ❼ Rust is under heavy development. ❼ The things described in this talk may not be true tomorrow. 3 / 25
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
❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Goals What do we want in a programming language? 4 / 25
❼ ❼ ❼ Introduction Rust Closures Conclusion Goals What do we want in a programming language? ❼ Fast: generates efficient machine code 4 / 25
❼ ❼ 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
❼ 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
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
❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Goals What do have? ❼ Firefox is in C++, which is Fast and Systemsy 5 / 25
❼ ❼ ❼ 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
❼ ❼ 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
❼ 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
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
Introduction Rust Closures Conclusion Rust a systems language pursuing the trifecta safe, concurrent, fast -lkuper 6 / 25
Introduction Rust Closures Conclusion Rust Design Status 7 / 25
❼ ❼ ❼ ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) 8 / 25
❼ ❼ ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention 8 / 25
❼ ❼ ❼ ❼ ❼ Introduction Rust Closures Conclusion Design Goals (straight from the docs) ❼ Compile-time error detection and prevention ❼ Run-time fault tolerance and containment 8 / 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 8 / 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 8 / 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 8 / 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
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
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
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
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
❼ ❼ ❼ Introduction Rust Closures Conclusion Status rustc ❼ Self-hosting rust compiler 12 / 25
❼ ❼ Introduction Rust Closures Conclusion Status rustc ❼ Self-hosting rust compiler ❼ Uses LLVM as a backend 12 / 25
❼ Introduction Rust Closures Conclusion Status rustc ❼ Self-hosting rust compiler ❼ Uses LLVM as a backend ❼ Handles polymorphism through type passing (blech) 12 / 25
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
❼ ❼ Introduction Rust Closures Conclusion Status The catch ❼ Not ready for prime time 13 / 25
❼ Introduction Rust Closures Conclusion Status The catch ❼ Not ready for prime time ❼ Lots of bugs and exposed sharp edges 13 / 25
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
Introduction Rust Closures Conclusion Closures What closures are Closures in rust 14 / 25
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
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
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
❼ Introduction Rust Closures Conclusion What closures are Traditional implementation ❼ Represent functions as a code pointer, environment pointer pair 18 / 25
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
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
❼ ❼ ❼ Introduction Rust Closures Conclusion Closures in rust Solutions ❼ Have two function types: block and fn 20 / 25
Recommend
More recommend