codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

12 / 58 topics
11Functions12Closures
Tutorials/Rust/Closures
🦀Rust

Closures

Updated 2026-05-15
10 min read

Closures

Introduction

In Rust, as in many other programming languages, closures are a powerful feature that allows you to encapsulate functionality together with the data it operates on. A closure is essentially an anonymous function that can capture its environment, meaning it can access variables from the scope in which it was defined.

Closures are particularly useful for writing concise and reusable code, especially when dealing with higher-order functions (functions that take other functions as arguments or return them). They provide a way to create functions on-the-fly and pass them around, making your code more flexible and expressive.

Concept

A closure in Rust can capture values from its environment in three ways:

  1. By reference (&T): The closure captures the variable by borrowing it immutably.
  2. By mutable reference (&mut T): The closure captures the variable by borrowing it mutably.
  3. By value (T): The closure takes ownership of the variable.

Rust's type system ensures that closures are safe and efficient, even when capturing variables from their environment. The compiler automatically infers the capture mode based on how the captured variables are used within the closure.

Examples

Let's explore some practical examples to understand closures better.

Example 1: Basic Closure

Here's a simple example of a closure that captures an immutable reference to a variable:

Rust
1fn main() {
2 let x = 4;
3 let closure = || println!("x is {}", x);
4 closure();
5}

In this example, the closure captures x by reference. When we call closure(), it prints the value of x.

Example 2: Mutable Capture

If you need to modify a captured variable, you can capture it by mutable reference:

Rust
1fn main() {
2 let mut x = 4;
3 let closure = || {
4 x += 1;
5 println!("x is now {}", x);
6 };
7 closure();
8}

Here, the closure captures x by mutable reference and increments its value.

Example 3: Moving Ownership

If you want to take ownership of a captured variable, you can capture it by value:

Rust
1fn main() {
2 let x = vec![1, 2, 3];
3 let closure = move || println!("x is {:?}", x);
4 closure();
5}

In this case, the closure takes ownership of x. After calling closure(), you cannot use x again in the main function.

Example 4: Higher-Order Function

Closures are often used with higher-order functions. Here's an example of a function that takes a closure as an argument:

Rust
1fn apply<F>(f: F)
2where
3 F: Fn(),
4{
5 f();
6}
7
8fn main() {
9 let x = 4;
10 let closure = || println!("x is {}", x);
11 apply(closure);
12}

The apply function takes a closure f as an argument and calls it. The closure captures x by reference.

Example 5: Closure with Parameters

Closures can also take parameters:

Rust
1fn main() {
2 let x = 4;
3 let closure = |y| println!("x is {}, y is {}", x, y);
4 closure(10);
5}

In this example, the closure captures x by reference and takes a parameter y.

What's Next?

Now that you have a good understanding of closures, it's important to learn about ownership in Rust. Ownership is a fundamental concept that governs how memory is managed and ensures safety without a garbage collector. Understanding ownership will help you write more robust and efficient Rust code.

In the next section, we'll dive deeper into ownership, exploring how Rust manages memory and ensuring safe concurrency.


PreviousFunctionsNext Ownership

Recommended Gear

FunctionsOwnership