3 ms·
> let add_closure = |a, b| a + b; let sixty_six = add_closure(42, 24); Woah woah, how did it not occur to me that I could just…construct a closure that directl
by FridgeSeal 3y ago
> let add_closure = |a, b| a + b;
let sixty_six = add_closure(42, 24);
Woah woah, how did it not occur to me that I could just…construct a closure that directly in Rust? This feels like one of those “incredibly obvious and reasonable in hindsight” things. I don’t know why I was labouring under the assumption they could only be invoked in very special and specific places, but it’s good to know I was wrong.
- vaylian 3y agoWelcome to functional programming, where functions are values.
- throwaway894345 3y agoIt becomes more painful when the closure is actually closing over something (especially if that thing is mutable), since the borrow checker gets involved.
- bsder 3y agoThis is a key thing to know, actually. Closures in Rust are different from named functions and that can trip you up. Closures in Rust also close over lifetime information in ways that named functions cannot do. This confused me for quite a while.
- duped 3y agoIf you want to do anything with the closure it gets a little tricker, for example if you did something naive like fn fn_that_takes_closure (f: fn (i32, 3i2) -> i32) { ... } let x = |a, b| a + b; fn_that_takes_closure(x); That would not compile, because `x` does not have the type `fn (i32, i32) -> i32` (that's reserved for "normal" functions). Every closure in Rust has a unique anonymous type, which means if you want to do anything with them besides calling them, you need to understand `Fn`, `FnOnce`, and `FnMut` traits and/or `dyn` trait objects. To write that above you'd need to do something like this: fn fn_that_takes_closure<F> (f: F) where F: Fn(i32, i32) -> i32
- proto_lambda 3y ago> That would not compile It does compile. > because `x` does not have the type `fn (i32, i32) -> i32` (that's reserved for "normal" functions). Closures can cast to function pointers just fine as long as they don't capture anything.
- abeppu 3y agoIsn't the distinguishing feature of a closure that it can capture stuff from the environment? > Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope. > The term closure is often used as a synonym for anonymous function, though strictly, an anonymous function is a function literal without a name, while a closure is an instance of a function, a value, whose non-local variables have been bound either to values or to storage locations (depending on the language; see the lexical environment section below). My understanding is that your use of 'capture' matches up with the the use of 'bound' in the description above. https://en.wikipedia.org/wiki/Closure_(computer_programming) https://en.wikipedia.org/wiki/Closure_(computer_programming)
- proto_lambda 3y agoIt can capture things, OP's example does not capture anything.
- evntdrvn 3y agopeople casually using the term closure for anonymous functions has caused so much confusion over the years lol
- xigoi 3y agoIf it doesn't capture anything, then it's not a closure.
- evntdrvn 3y agoThere’s so much confusion out there due to people throwing out the term “closure” imprecisely, ugh