Rust - Concept of Capturing Variables with Closers Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Rust, we can capture variables via Closures. Closures are anonymous functions that help us in saving variables or pass arguments to other functions. Closures can be created from one place and then can be called anywhere and they can capture values from their defined scope. While capturing variables, Rust uses the following methods to do this: by reference: (&T)by value: Tby mutable reference: (&mut T)Example 1: Rust // Rust code by reference fn main() { let var = String::from("GeeksforGeeks"); let name = || println!("`Name`: {}", var); name(); //calling the closure } Output: Explanation: In this example, a closure is used to print the value that borrows (`&`) 'var' and stores the borrow and the closure in the 'name' variable. This would remain there until 'name' is used previously. As we know that println! requires only arguments that reference immutable therefore there are no restrictions imposed. The closure name is called using the borrow. Example 2: Rust // Rust code for by value T fn main() { let mut count = 0; let mut i= 0; let mut count_closure = || { count += 10; println!("`count:{}, value: {}`", i, count); i+=1; }; // Calling the closure 'count_closure' using a mutable borrow. count_closure(); count_closure(); count_closure(); } Output: Explanation: In this example, we have used a closure 'count_closure' that can take either a mutable count incrementor or a counter variable (non-mutable). We have used mut since calling our closure mutates with the closure that requires an out parameter. Here, we have declared an I that stores the number of times the closure is called and is incremented accordingly. Example 3: Rust // Rust code for by mutable reference: (&mut T) fn main() { use std::mem; let moving_variable = Box::new(5); let variable_used = || { println!("`movable variable value`: {:?}", moving_variable); mem::drop(moving_variable); }; variable_used(); } Output: Explanation: In this example, we have imported the std::mem trait that helps us in managing the memory once the variable is dereferenced. We have created a variable named moving_variable and have allocated memory on the heap that has a value of 5. As mem::drop requires a trait (T) therefore this takes a value. A copy-type variable would copy into the closure leaving the original closure unaffected. variable_used consumes the variable only once hence it is called one time only. Comment More infoAdvertise with us Next Article How to avoid unused Variable warning in Rust? S sayanc170 Follow Improve Article Tags : Technical Scripter Rust Technical Scripter 2022 Rust-basics Similar Reads Rust - Concept of Scope, Block, and Shadowing Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially safe concurrency by using a borrow checker and ownership to validate references. In this article, we will discuss the concepts behind the scope, block, and shadowing in Rust progra 2 min read Rust - A Case of Safe Concurrency Before looking at Rust itself, let's go back to 1992. Guido van Rossum, in an attempt to tackle race condition in CPython interpreter, added a lock famously known as the Global Interpreter Lock or GIL for short. Two and a half decades later, this was one of the main shortcomings for the Python inter 5 min read Rust - A Case of Safe Concurrency Before looking at Rust itself, let's go back to 1992. Guido van Rossum, in an attempt to tackle race condition in CPython interpreter, added a lock famously known as the Global Interpreter Lock or GIL for short. Two and a half decades later, this was one of the main shortcomings for the Python inter 5 min read Variables in Rust Variables are memory addresses used to store information to be referenced and manipulated in programs. They also provide a way of defining data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that 4 min read How to avoid unused Variable warning in Rust? Rust is a modern system programming language mainly focusing on safety, performance, and concurrency. It eliminates many classes of bugs at compile time through Its ownership system which ensures memory safety without a garbage collector. In this article, we explain how to avoid unused variable warn 4 min read Perl - Use of Capturing in Regular Expressions A regular expression or a regex is a string of characters that define the pattern that we are viewing. It is a special string describing a search pattern present inside a given text. Perl allows us to group portions of these patterns together into a subpattern and also remembers the string matched b 3 min read Like