9951 explained code solutions for 126 technologies


rustHow to borrow closure in Rust


Closure is a feature of Rust that allows a function to capture and use variables from its environment. It is a powerful tool for writing concise and expressive code.

Example code

fn main() {
    let x = 5;
    let closure = || x * x;
    println!("{}", closure());
}

Output example

25

Code explanation

  1. let x = 5; - declares a variable x with value 5
  2. let closure = || x * x; - declares a closure closure which captures the variable x from its environment and multiplies it by itself
  3. println!("{}", closure()); - prints the result of calling the closure closure

Helpful links

Edit this code on GitHub