← All Exercises

Closures and Capture

intermediaterust~20min

Background

Closures in Rust provide anonymous function syntax with variable capture semantics.

Problem

Create closures that capture variables by reference and by move, understanding Fn trait bounds.

Input

Variables to capture

Output

Closure with captured state

Sample tests

Sample 1

Input: let x = 5; let f = || x + 1;
Expected: f() = 6

Your solution

Learning objective

Learn closure capture modes and Fn trait family