Note: This question was asked before Rust's first stable release. There have been lots of changes since and the syntax used in the function is not even valid anymore. Still, Shepmaster's answer is excellent and makes this question worth keeping.
Finally unboxed closures have landed, so I am experimenting with them to see what you can do.
I have this simple function:
fn make_adder(a: int, b: int) -> || -> int {
|| a + b
}
However, I get a missing lifetime specifier [E0106]
error. I have tried to fix this by changing the return type to ||: 'static -> int
, but then I get another error cannot infer an appropriate lifetime due to conflicting requirements
.
If I understand correctly, the closure is unboxed so it owns a
and b
. It seems very strange to me that it needs a lifetime. How can I fix this?
question from:
https://stackoverflow.com/questions/25445761/returning-a-closure-from-a-function 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…