blob: 1c424062338d024c9f6eb5063dc27f2d4e5b8cf5 [file] [log] [blame] [view] [edit]
# Functional Language Features: Iterators and Closures
Rusts design has taken inspiration from many existing languages and techniques,
and one significant influence is _functional programming_. Programming in a
functional style often includes using functions as values by passing them in
arguments, returning them from other functions, assigning them to variables for
later execution, and so forth.
In this chapter, we wont debate the issue of what functional programming is or
isnt but will instead discuss some features of Rust that are similar to
features in many languages often referred to as functional.
More specifically, well cover:
* _Closures_, a function-like construct you can store in a variable
* _Iterators_, a way of processing a series of elements
* How to use closures and iterators to improve the I/O project in Chapter 12
* The performance of closures and iterators (Spoiler alert: theyre faster than
you might think!)
Weve already covered some other Rust features, such as pattern matching and
enums, that are also influenced by the functional style. Because mastering
closures and iterators is an important part of writing idiomatic, fast Rust
code, well devote this entire chapter to them.