[Rust Guide] 13.4. Capturing the Environment With Closures
13.4.0 Before We Begin During its design, Rust drew inspiration from many languages, and functional programming had a particularly strong influence on Rust. Functional programming often includes passing functions as values to parameters, returning them from other functions, assigning them to variables for later execution, and so on. In this chapter, we will discuss some Rust features that are similar to what many languages call functional features: Closures (this article) Iterators Improving the I/O Project with Closures and Iterators Performance of Closures and Iterators If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series. 13.4.1 Closures Can Capture Their Environment Closures have a capability that functions do not: a closure can access variables in the scope where it is defined. Take a look at an example: fn main () { let x = 4 ; let equal_to_x = | z | z == x ; let y = 4 ; assert! ( equal_to_x ( y )); } The closure part is: let equal_to_x = | z | z == x ; Some people may find it hard to distinguish the roles of = and == here, so let’s rewrite it another way: let equal_to_x = | z | { z == x ; } In other words, the closure takes z as its parameter, compares it with x (which is 4, because x = 4 was defined above), and returns a boolean. If they are equal, the result is true ; otherwise it is false . Here the closure directly accesses the variable x in the same scope, which functions cannot do. But this feature has a cost: it introduces memory overhead . In most cases we do not need a closure to capture its environment, and we do not want the extra overhead either. That is why functions are not allowed to capture variables from the environment, and defining and using a function never introduces this kind of overhead. 13.4.2 How Closures Capture Values From Their Environment Closures capture values from the environment in three ways, just like functions receive parameters in three ways: Taking ownership, whose trait