>();
```
---
## `fold`
```rust
fn fold(self, init: B, f: F) -> B
where F: FnMut(B, Self::Item) -> B;
let vs = vec![1,2,3,4,5];
let sum = vs.iter().fold(0, |acc, &x| acc + x);
assert_eq!(sum, 15);
```
- `fold` "folds up" an iterator into a single value.
- Sometimes called `reduce` or `inject` in other languages.
- `fold` takes two arguments:
- An initial value or "accumulator" (`acc` above) of type `B`.
- A function that takes a `B` and the type inside the iterator (`Item`) and
returns a `B`.
- Rust doesn't do tail-recursion (yet), so `fold` is implemented iteratively.
- [See here](https://github.com/rust-lang/rust/issues/217) for more info.
---
## `filter`
```rust
fn filter(self, predicate: P) -> Filter
where P: FnMut(&Self::Item) -> bool;
```
- `filter` takes a predicate function `P` and removes anything that doesn't pass
the predicate.
- `filter` returns a `Filter`, so you need to `collect` it to get a new
collection.
---
## `skip`
```rust
fn skip(self, n: usize) -> Skip;
```
- Creates an iterator that skips its first `n` elements.
---
## `zip`
```rust
fn zip(self, other: U) -> Zip
where U: IntoIterator;
```
- Takes two iterators and zips them into a single iterator.
- Invoked like `a.iter().zip(b.iter())`.
- Returns pairs of items like `(ai, bi)`.
- The shorter iterator of the two wins for stopping iteration.
---
## `any` & `all`
```rust
fn any(&mut self, f: F) -> bool
where F: FnMut(Self::Item) -> bool;
fn all(&mut self, f: F) -> bool
where F: FnMut(Self::Item) -> bool;
```
- `any` tests if any element in the iterator matches the input function
- `all` tests all elements in the iterator match the input function
- Logical OR vs. logical AND.
---
## `enumerate`
```rust
fn enumerate(self) -> Enumerate;
```
- Want to iterate over a collection by item and index?
- Use `enumerate`!
- This iterator returns `(index, value)` pairs.
- `index` is the `usize` index of `value` in the collection.
---
## Iterator Adapters
- Adapters operate on an iterator and return a new iterator.
- Adapters are often _lazy_ -- they don't evaluate unless you force them to!
- You must explicitly call some iterator consumer on an adapter or use it in a
`for` loop to cause it to evaluate.
---
## `map`
```rust
fn map(self, f: F) -> Map
where F: FnMut(Self::Item) -> B;
let vs = vec![1,2,3,4,5];
let twice_vs: Vec<_> = vs.iter().map(|x| x * 2).collect();
```
- `map` takes a function and creates an iterator that calls the function on each
element
- Abstractly, it takes a `Collection` and a function of `A -> B` and
returns a `Collection`
- (`Collection` is not a real type)
---
## `take` & `take_while`
```rust
fn take(self, n: usize) -> Take;
fn take_while(self, predicate: P) -> TakeWhile
where P: FnMut(&Self::Item) -> bool;
```
- `take` creates an iterator that yields its first `n` elements.
- `take_while` takes a closure as an argument, and iterates until the closure
returns `false`.
- Can be used on infinite ranges to produce finite enumerations:
```rust
for i in (0..).take(5) {
println!("{}", i); // Prints 0 1 2 3 4
}
```
---
## `cloned`
```rust
fn cloned<'a, T>(self) -> Cloned
where T: 'a + Clone, Self: Iterator- ;
```
- Creates an iterator which calls `clone` on all of its elements.
- Abstracts the common pattern `vs.iter().map(|v| v.clone())`.
- Useful when you have an iterator over `&T`, but need one over `T`.
---
## `drain`
- Not actually an `Iterator` method, but is very similar.
- Calling `drain()` on a collection removes and returns some or all elements.
- e.g. `Vec::drain(&mut self, range: R)` removes and returns a range out of a vector.
---
## Iterators
- There are many more `Iterator` methods we didn't cover.
- Take a look at [the docs](https://doc.rust-lang.org/std/iter/trait.Iterator.html) for the rest.