Rust, abstracted: lambda lifting

What is lambda lifting?

Lambda Lifting is a procedure in functional programming where a lambda, otherwise known as a function, is moved from an inner scope, to an outer scope. This move is often done for optimization purposes, or also for necessary implementation purposes.

Does Rust do lambda lifting automatically?

Rust does not perform lambda lifting as an optimization. Rust does however move lambda bodies into the global scope for implementation purposes. This creates a flat global function scope that plays well with LLVM IR or other possible backends that prefer to work with code blocks than to work with nested symbols.

What does manual lambda lifting look like?

Below we manually implement lambda lifting for a simple function that captures x, then multiplies by a value y.

fn lifted(x: u64, y: u64) -> u64 {
   x*y
}

fn main() {
   let x = 5;
   println!("{}", (|y| x*y)(6));
   println!("{}", lifted(6,7));
}