Rust for C++

Closures Internals

Rust
let mut vector = Vec::new();
let mut closure = || vector.push(1);
closure();
Compiled
struct Environment<'v> {
    v: &'v mut Vec<i32>
}

impl<'v> FnMut() for Environment<'v> {
    fn call_mut(&mut self) {
        self.v.push(1)
    }
}

let mut vector = Vec::new();
let mut closure = Environment{v: &vector};
closure();