Capture with closures
A closure writes |parameters| expression or returns a block. Types may be inferred from context. Captured bindings are shared by reference, so mutations inside and outside observe the same value.
A let-bound closure may call itself. Give recursive closures an explicit return type when the base case cannot make it obvious.
fn main() {
let mut count = 4
let bump = || -> int { count += 1; count }
println(bump())
println(bump())
println(count)
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: 5 6 6
Ctrl/⌘+Enter runs the current lesson.