Re:Announcing Rust 1.18
by kibwen on Thursday June 08, 2017 @01:51PM
(#14515810)
> you cannot write a function that returns a closure
This is true for your specific example, but in the interest of completeness there are cases where such a thing is possible. The full story is that you can't name the closure's type, so you can only write the type signature in certain cases. For example, the following function returns a stack-allocated closure just fine today (with example usage to prove it):
fn foo<F: Fn(i32)->i32>(bar: F) -> F {
bar
}
fn main() {
let x = |y| { y + 2 };
let z = foo(x);
z(5);
}
This is mostly pedantry, though, because it's true in that the majority of the time that you want to return a closure it's a closure that was created within that function, not passed in. This is where `impl Trait` is crucial.
This is true for your specific example, but in the interest of completeness there are cases where such a thing is possible. The full story is that you can't name the closure's type, so you can only write the type signature in certain cases. For example, the following function returns a stack-allocated closure just fine today (with example usage to prove it):
This is mostly pedantry, though, because it's true in that the majority of the time that you want to return a closure it's a closure that was created within that function, not passed in. This is where `impl Trait` is crucial.