Skip to main content
freelanceshack.com

Back to all posts

How to Use Closures In Rust?

Published on
5 min read
How to Use Closures In Rust? image

Best Rust Programming Guides to Buy in October 2025

1 The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

BUY & SAVE
$30.13 $49.99
Save 40%
The Rust Programming Language, 2nd Edition
2 Programming Rust: Fast, Safe Systems Development

Programming Rust: Fast, Safe Systems Development

BUY & SAVE
$43.99 $79.99
Save 45%
Programming Rust: Fast, Safe Systems Development
3 Rust for Rustaceans: Idiomatic Programming for Experienced Developers

Rust for Rustaceans: Idiomatic Programming for Experienced Developers

BUY & SAVE
$29.99 $49.99
Save 40%
Rust for Rustaceans: Idiomatic Programming for Experienced Developers
4 Rust in Action

Rust in Action

BUY & SAVE
$51.42 $59.99
Save 14%
Rust in Action
5 Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

BUY & SAVE
$47.04 $59.95
Save 22%
Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)
6 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
7 The Rust Programming Language

The Rust Programming Language

BUY & SAVE
$16.92 $39.95
Save 58%
The Rust Programming Language
8 Rust Atomics and Locks: Low-Level Concurrency in Practice

Rust Atomics and Locks: Low-Level Concurrency in Practice

BUY & SAVE
$33.13 $55.99
Save 41%
Rust Atomics and Locks: Low-Level Concurrency in Practice
9 Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes

Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes

BUY & SAVE
$28.90 $49.99
Save 42%
Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes
10 Refactoring to Rust

Refactoring to Rust

BUY & SAVE
$49.99
Refactoring to Rust
+
ONE MORE?

Closures in Rust are anonymous functions that can be assigned to variables or passed as arguments to other functions. They can capture values from their surrounding environment and are a powerful tool for writing concise and flexible code.

To define a closure, you enclose the function body within curly braces ({}) and use the [move](https://stlplaces.com/blog/how-to-move-up-move-down-a-windows-languages-with) keyword to specify that it should capture values by moving them into the closure. Here's a basic example:

let multiply = |x, y| x * y; // A closure that multiplies two numbers

let result = multiply(5, 3); println!("Result: {}", result); // Output: Result: 15

In this example, the closure multiply takes two arguments x and y and returns their product. The closure implicitly captures the variables x and y from its surrounding environment.

Closures can also capture variables by reference or mutable reference using the & or &mut syntax. This allows modifying variables outside the closure. Here's an example using captured references:

let mut counter = 0;

let increment = || { counter += 1; println!("Counter: {}", counter); };

increment(); increment();

In this code, the closure increment captures the counter variable by mutable reference and increments it. The output will be:

Counter: 1 Counter: 2

To use closures as arguments, you can pass them directly or in higher-order functions like map, filter, or fold. Here's an example using map:

let numbers = vec![1, 2, 3, 4, 5];

let squared_numbers: Vec<_> = numbers.iter().map(|x| x * x).collect();

println!("Squared numbers: {:?}", squared_numbers); // Output: Squared numbers: [1, 4, 9, 16, 25]

In this code, the closure |x| x * x is passed to the map function and applied to each element of the numbers vector, generating a new vector of squared numbers.

Closures in Rust are flexible and provide a concise way to define and use anonymous functions. They can capture values from their environment, modify variables outside their scope, and be passed as arguments to other functions, making them an essential tool for functional programming and writing expressive code.

What is the shorthand syntax for closures in Rust?

The shorthand syntax for closures in Rust is called the "move closure syntax". It is used when you want the closure to take ownership of the variables it captures instead of borrowing them.

The syntax for a closure using move closure syntax is:

move |parameters| { /* function body */ }

Here's an example of a closure that takes ownership of the variable x using move closure syntax:

let x = 5; let closure = move || { println!("Value of x: {}", x); };

In this example, the closure takes ownership of the variable x and can use it even after x goes out of scope.

What is the purpose of "FnMut" trait in Rust closures?

The "FnMut" trait in Rust closures is used to indicate that a closure captures its environment and can modify it. It is a marker trait that denotes that the closure is mutable and can mutate the captured variables or data in its environment.

The "FnMut" trait is one of the three closure traits in Rust, along with "Fn" and "FnOnce". Each trait represents a different level of access and mutability for the closure. "FnMut" indicates that the closure can be called mutably, allowing it to both read and modify the captured variables.

By using the "FnMut" trait, Rust allows closures to be used in a flexible manner and enables them to adapt to different requirements. This trait allows closures to capture and modify external variables, making them versatile and useful in scenarios where mutable access is required.

What are the different types of closures in Rust?

In Rust, there are two different types of closures:

  1. Function-like closures: These closures are defined using the |...| {...} syntax and are similar to function definitions. They can take arguments and return a value. Function-like closures are also known as lambda functions. Here is an example:

let add = |a, b| a + b; println!("Sum: {}", add(2, 3)); // Output: Sum: 5

  1. Fn, FnMut, and FnOnce closures: These closures are defined using the Fn, FnMut, and FnOnce traits and are known as "function pointers." They can capture variables from their surrounding environment and are more flexible than function-like closures. The distinctions between these traits are related to how the closure mutates captured variables. Here is an example:

fn main() { let number = 5; let inc = move |x| x + number; println!("Incremented: {}", inc(10)); // Output: Incremented: 15

// A Move closure is used when you want to transfer ownership of captured variables.
// FnOnce - consumes the variables it captures
// FnMut - can modify the captured variables
// Fn - read-only access to captured variables is allowed

}

These closures can be used interchangeably in many cases, depending on the requirements of the situation. Additionally, closures in Rust are implemented using anonymous types that implement the appropriate trait (Fn, FnMut, or FnOnce), allowing for more flexibility and optimization.