[rust-dev] returning functions in rust
pwalton at mozilla.com
Fri Jan 10 10:57:43 PST 2014
It doesn't exist, outside of traits. Unboxed closures will probably make it possible to express once again though.
Kevin Ballard <kevin at sb.org> wrote:
>On Jan 10, 2014, at 10:18 AM, Patrick Walton <pcwalton at mozilla.com>
>> On 1/10/14 7:20 AM, Nakamura wrote:
>>> I'm new to rust, and tried going through some of the examples from
>>> OS class that was taught in rust. However, I got tripped up by
>>> exercise, "make a function that takes an integer n and another
>>> |x| -> x, and returns a function that is n applications of the
>>> I've been trying to see what the limits of rust are if you are using
>>> without the runtime/managed pointers etc, and it seems like I
>>> against one of the limits when trying to return functions. The big
>>> question is where to allocate the new function I want to return.
>> Rust won't automatically allocate closed-over variables on the heap.
>This limits the ability to write code like this naturally, but you get
>the benefit that all allocations are immediately visible and under the
>control of the programmer.
>> I would not suggest trying to use managed pointers for this. Instead
>I would suggest `proc`, which is allocated on the exchange heap and can
>close over variables. This should be sufficient for the question as
>posed, as long as the "another function" has a `'static` bound
>(`'static |x| -> x`).
>Procs can only be called once, though, which is a bit of a limitation.
>Does ~|| -> T no longer exist?
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Rust-dev