Oh, cool. Instead of returning the closure from a function, I'd rather use
it in a call to map(), then return that Map struct. Something like:

fn map_add<I: Iterator<int>>(it: I, x: int) -> Map<...> {
    it.map(|&: y: int| x + y)
}

So does this mean I just need to wait until the map() function accepts
unboxed closures?

On Tue, Sep 9, 2014 at 5:00 PM, Vladimir Matveev <dpx.infin...@gmail.com>
wrote:

> Hi, Allen!
>
> In fact, it is possible to do it now without DST, using unboxed closures
> only:
>
> #![feature(unboxed_closures, unboxed_closure_sugar)]
>
> fn make_adder(x: int) -> Box<|&: int| -> int> {
>     box |&: y: int| x + y
> }
>
> fn main() {
>     let f = make_adder(3);
>     println!("{}", f.call((4i,)));
> }
>
> Test it here: http://is.gd/mCJ6Dh
>
> This code employs the idea that unboxed closure is just an instance of
> some anonymous struct implementing one of Fn* traits, so we can just
> return a boxed trait object representing the closure. The call syntax
> is ugly, however, but this should change in the nearest future (though
> explicit dereferencing, like (*f)(x), will likely be needed anyway).
>
> 2014-09-10 0:39 GMT+04:00 Allen Welkie <allen.wel...@gmail.com>:
> > In this stackoverflow question:
> >
> >
> http://stackoverflow.com/questions/21130272/return-a-closure-from-a-function
> >
> > Chris Morgan mentions the possibility of adding closures with owned
> > environments with the DST implementation. Is this being done with the
> > current DST effort? If not, are there plans to add it?
> >
> > _______________________________________________
> > Rust-dev mailing list
> > Rust-dev@mozilla.org
> > https://mail.mozilla.org/listinfo/rust-dev
> >
>
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to