2 ms·
> You'd like for this to optimize down to a function call, without saving any state from the outer scope. Don't know if Rust does this yet. It does. Rust closu
by Manishearth 11y ago
> You'd like for this to optimize down to a function call, without saving any state from the outer scope. Don't know if Rust does this yet.
It does. Rust closures translate to a stack struct containing the capture and a method call on that struct. In this case, the struct is zero-sized, so it doesn't exist, so it's just a function call. Additionally, unwrap_or_else will probably be monomorphised and then inlined, so there will be no call at all.
"closure" is just the term for the syntax.
> You can't pass a pointer to a function.
Sure you can.
fn main() {
Ok(1).unwrap_or_else(inc);
}
fn inc(x: u8) -> u8 {
x+1
}
Both functions and closures implement the Fn* traits