The corresponding Rust closure is not saved in a let binding, instead, on every closure call, the body is fully inlined.
template <typename F> int apply(F fn, int x) { return fn(x); }
int main() {
int base = 10;
auto add_base = [&base](int x) { return x + base; };
return apply(add_base, 5);
}
becomes
pub unsafe fn apply_0(mut fn_: impl Fn(i32) -> i32, mut x: i32) -> i32 {
return fn_(x);
}
unsafe fn main_0() -> i32 {
let mut base: i32 = 10;
return apply_0(
(|x: i32| {
return x + base;
})
.clone(),
5,
);
}
A stored closure that captures locals by reference keeps the local borrowed for as long as the closure lives. So let foo = || { a += 1; a }; return foo() + a; does not compile
The corresponding Rust closure is not saved in a let binding, instead, on every closure call, the body is fully inlined.
becomes
A stored closure that captures locals by reference keeps the local borrowed for as long as the closure lives. So
let foo = || { a += 1; a }; return foo() + a;does not compile