8 ms·
Why Rust closures are somewhat hard
- t3hz0r 7y agoNote that the workaround to force `change_x` out of scope in the section 'Implications of “Closures are Structs”' is no longer necessary with non-lexical lifetimes. So Rust closures are (somewhat) less hard now :)
- rafaelvasco 7y agoI feel most things in Rust are harder to do, at least at first sight. A compromise for the enhanced safeness I guess. Could give it a try again sometime.
- saghm 7y agoIn general, it's not the safety alone, but the combination of safety and eschewing implicit runtime costs. It would be 100% safe for Rust to always implicitly box closures; it just wouldn't be as efficient in the cases where boxing them isn't needed.
- taberiand 7y agoIt seems to me that it's not that Rust is hard, it's that all programming is hard but Rust exposes the complexity so we can make a more informed decision about the trade offs of safety and performance etc, while other languages tend to hide the complexity (null reference, race conditions, etc).
- rafaelvasco 7y agoYeah. Makes sense. Rust just explicitly exposes certain features so that the programmer has more control.
- efaref 7y agoI've found that once you fully get this, Rust is actually easier to write code with. Especially refactoring. You can make sweeping changes to complex code and be satisfied that once it compiles it will most likely work. Fearless refactoring is one of the prime benefits of Rust.
- justwalt 7y agoIn my experience that’s true. But once you code in it consistently for some appreciable amount of time, you start to think in ways that the compiler will be happy with and that make sense from a safety standpoint. It’s getting to that point that requires a bit of work.
- deleted 7y ago[deleted]
- cobbzilla 7y agoThe fact that you can even do stuff like this in Rust is amazing. It’s simultaneously: relatively clean code, very efficient and type-safe. I like it.
- capableweb 7y agoNot sure I agree with "relatively clean code" when one of the examples show "fn compose <T>(f1: impl Fn(T)->T, f2: impl Fn(T)->T) -> impl Fn(T)->T {" which is just a mix-match of keywords and other things, with tons of syntax embedded in just one line. But as always, depends on where you come from. I mostly deal with lisp languages nowadays, so guessing it's just my view that the line quoted above seems complex enough to not be interested one bit in Rust.
- optimuspaul 7y agoWhile that may be an extreme example I agree. nothing clean about that code. I've only just begun to dive into rust, but I feel like I'd need a concordance to navigate the meaning of that snippet alone. Bt then again, maybe it depends on where you come from.
- bschwindHN 7y agoTakes a week or two of studying the language. It's well worth it, in my opinion.
- aratakareigen 7y agoThat line is extremely simple to me. What makes it seem like a "mix-match" of symbols to you? The equivalent in Haskell would be "compose :: (a -> a) -> (a -> a) -> a -> a", which lacks "impl" because it boxes all closures (each closure in Rust has its own type), and lacks "Fn" because it doesn't care about what kind of access the closure needs to its context (Rust distinguishes between Fn, FnMut and FnOnce). Still, the Rust is only a tiny bit more complicated than the Haskell.
- the8472 7y agoTrait aliases will make some of those unwieldly declarations a bit easier on the eyes #![feature(trait_alias)] trait Map<T> = Fn(T) -> T; fn compose <T>(f1: impl Map<T>, f2: impl Map<T>) -> impl Map<T> { |n| f1(f2(n)) }
- jimbob45 7y agoI don't understand the intuition of closures and they turn me off to languages immediately. They feel like a hack from someone who didn't want to store a copy of a parent-scope variable within a function. The idea that I can touch variables that have gone out of scope (and that have ostensibly been GC'd) makes me feel that it is impossible to reason about variable lifetimes when dealing with closures. Is there some perspective I'm missing here? Is it literally just the language invisibly adding the parent-scope variables and their values to the top of my function?
- merlincorey 7y agoThere are indeed some nuances you are missing as well as at least one misconception you are holding: > The idea that I can touch variables that have gone out of scope (and that have ostensibly been GC'd) You can't touch variables that have been garbage collected, indeed. A variable captured in a closure will have a reference to it and therefore not be garbage collected until the closure itself goes out of scope and is garbage collected. Closures allow encoding some state into a function, which is especially useful for higher order functions that return specialized functions, for example. This is, for example, how decorators in Python work - a function is captured in a closure, which is then referenced and called by a new function wrapping the original function.
- chubot 7y agoI don't have a problem with the intuition but I'm with you on programming style. It's sort of a contrary opinion but I prefer to be explicit about state, and closures are sort of silently bundling up state for you behind the scenes. It's not very apparent from the syntax. I think languages should have a lighter-weight syntax for classes and that would subsume many use cases for closures. ----- While I've never programmed in PHP, it appears PHP actually has a pretty nice solution for this problem! You declare the variables captured using 'use': https://stackoverflow.com/questions/1065188/in-php-what-is-a-closure-and-why-does-it-use-the-use-identifier https://stackoverflow.com/questions/1065188/in-php-what-is-a... $callback = function ($quantity, $product) use ($tax, &$total) In other languages the capture of 'tax' and 'total' are implicit.
- Animats 7y agoThere's a lot of confusion about this. A lambda is just a function without a name. (This feature tends to come with special syntax, although it doesn't have to.) A nested function is a function defined inside another function which can access the variables of the enclosing function. (A nested function can be a lambda, but it doesn't have to be. Some languages have named nested functions. A lambda doesn't have to be a nested function; it doesn't have to pull in any variables from an outer scope.) A closure is a nested function which can outlive the outer function, keeping its data alive. (A closure can be named, the usual case in Python and Javascript, or anonymous. So a closure need not be a lambda.) Closures are easy to implement in garbage collected languages, but hard in explicitly allocated ones, because extending the lifetime of the imported data gets complicated. So the options are: - Lambda without any external data access -- typical use, comparison function for a sort. - Lambda with external data access, but not outliving its enclosing function - typical use, iteration expression - Named function with no external data access. Typical use, a local function in languages that don't do local functions well, such as C. - Named function with external data access, not outliving its enclosing function. Typical use, internal function within a function to avoid passing extra parameters. - Named function with external data access, outliving its enclosing function. A true closure, but not a lambda. Typical use, saving state for a callback in Javascript by passing the function to something that will save it and invoke it later. An object, really. This was how LISP did objects. - Lambda function with external data access, outliving its enclosing function. A true closure. Same uses as above, but in different languages. Most languages offer some subset of these six options.
- deleted 7y ago[deleted]
- jarjoura 7y agoI think the OP makes it clear that this nuance is actually quite tricky in a native systems language like Rust. A good example in C++ would be: - A function (non capturing/lambda) is created with auto f = [] { ... }; - A function (capturing/closure) is created with auto s = "..."; auto f = [&] { s; ... }; In source code both of those look similar and you can even define them with the same type: std::function<void()> However, the resulting assembly for both couldn't be more different. The Lambda case is as raw as any normal C function type, which the closure case creates a C++ class that closes over the outer method's state. If you're not a seasoned C++ engineer, this nuance will be lost on you. It's fair to say that everyone should understand Lambda Calculus rules, but that makes the language less accessible to new-comers. In the case for non-systems languages, you can blur the lines with ease, but something Rust and C++ just cannot afford to do. That makes understanding the nuance important to be effective.
- chrismorgan 7y ago> With Rust 1.26, there’s a simpler but completely equivalent notation: fn new_invoke(f: impl Fn(f64)->f64, x: f64) -> f64 { f(x) } That actually isn’t completely equivalent. With the former example, `invoke::<_>(x)` works, but with impl, `new_invoke::<_>(x)` doesn’t work. This is a deliberate aspect of the design of impl in argument position, and part of the reason why it can be better to avoid it in libraries. In the case of functions, this difference probably doesn’t matter, because you normally can’t name the type of a function anyway, and are extremely unlikely to wish to; but in other cases being able to type the turbofish can matter for ergonomics. As an arbitrary example, take std::convert::Into::into: the type parameter is on the trait rather than the method, so you can’t do `x.into::<T>()`, but if you need to constrain the type you must do so otherwise, e.g. `let y: T = x.into();` or `<_ as Into<T>>::into(x)`. (In that case in particular, you’d write `T::from(x)` instead, but there won’t always be such an ergonomic replacement.)
- lmm 7y agoIt feels like the part that's missing is the ability to abstract over these different types of function, polymorphically. At least, that seems to be where things fall down when we try to talk about e.g. implementing a Functor trait in Rust. As a concrete example, the `compose` method given should clearly be the same for `Fn`, `FnOnce`, and `FnMut`. Can we write it once and reuse it for `Fn`, `FnOnce` and `FnMut`? If not, why not?
- kccqzy 7y agoBecause Rust doesn't have higher-kinded types. Well it does but it doesn't allow you to define or implement traits for them. Also, what happens when you compose one function with FnMut and another with just Fn? The answer is clearly FnMut. These three traits are related by a subtyping relationship. I don't know enough Rust to answer the question of whether such a hypothetical compose function can return the right one from this hierarchy.
- deleted 7y ago[deleted]
- Koshkin 7y agoWell, looks like everything in Rust is somewhat hard.
- zelly 7y agoEverything that's hard in Rust could be solved by GC. With Rust, programmers have to spend most of their mental energy worrying about management of memory, which has largely been automated already. I always go back to this quote from Andrei Alexandrescu (creator of D): A disharmonic personality. Reading any amount of Rust code evokes the joke "friends don't let friends skip leg day" and the comic imagery of men with hulky torsos resting on skinny legs. Rust puts safe, precise memory management front and center of everything. Unfortunately, that's seldom the problem domain, which means a large fraction of the thinking and coding are dedicated to essentially a clerical job (which GC languages actually automate out of sight). Safe, deterministic memory reclamation is a hard problem, but is not the only problem or even the most important problem in a program. Therefore Rust ends up expending a disproportionately large language design real estate on this one matter. It will be interesting to see how Rust starts bulking up other aspects of the language; the only solution is to grow the language, but then the question remains whether abstraction can help the pesky necessity to deal with resources at all levels. RAII and borrow checking is a crutch. You just limited the set of programs you can write to those that can be written in the block-lifetime-scoped manner, which is smaller than the set of good programs (see: pretty much any graph-heavy data structures). The limitations of this programming model show up everywhere, like in the way closures have to be implemented. There will be more innovation in GC that will make manual memory management even more useless. In a lot of cases the JVM does a better job of freeing memory than a programmer. I don't want to spend my time programming worrying about the same thing (memory) that K&R did in the 70s. I don't want to bet against innovation and technology.
- jcranmer 7y ago> You just limited the set of programs you can write to those that can be written in the block-lifetime-scoped manner, which is smaller than the set of good programs (see: pretty much any graph-heavy data structures). Given that most of my Rust programs have involved graph-heavy data structures, I'd like to see you explain why it's impossible for me to have written what I have written.
- zelly 7y ago
- hechang1997 7y agoThis article does a great job explaining the subtleties about rust closures. A subtle point often missed is that if you want the anonymous structure of your closure itself to own a value, so that you can have a closure with a `'static` lifetime (which lives forever) that still could be called multiple times, you must use move keyword. If you just move the values into the closure manually, your closure will be a `FnOnce` since the compiler will mistakenly believe that you want to move that value each time the closure gets invoked, which can only happen once if the moved value doesn't implement `Copy`. In constrast, if you use the `move` keyword, compiler will correctly move the value into the anonymous structure of the closure itself during its construction, and the closure will be a `FuMut` and/or `Fn` as long as you don't consume the said value in the closure.
- hechang1997 7y agohttps://www.snoyman.com/blog/2018/11/rust-crash-course-05-rule-of-three https://www.snoyman.com/blog/2018/11/rust-crash-course-05-ru...