...

/

Higher-order Functions: Part II

Higher-order Functions: Part II

Let's review the most common Higher Order Functions in Rust.

The most common HOFs

Higher-order functions are common in Rust in two different fields of application:

  • Iterators
  • Options

Knowing the most common of these functions and how to use them can give our code a performance boost.

Helpful tips for the rest of the course

  1. Play with the code. The more you play, the more you will understand HOF and by extension FP.
  2. Keep this lesson open while you’re going through the rest of the course. These core concepts will be helpful all the way to the end.
  3. The list below not only contains HOFs, but also some other useful methods for dealing with iterators, options, and results.
  4. Most Result have some HOFs. However, if you know the HOFs present in Option, then you already know most of the HOFs relevant to Result. Rust’s official documentation has all the details.

Iterators

The following methods are the most commonly used, but the Rust documents contain even more methods.

Navigation

In order to navigate through a collection, we can use the following methods.

skip()

This method creates a new iterator that skips the first n elements:

Press + to interact
let a = [1, 2, 3, 4, 5];
let a_iter = a.iter().skip(2);
let b: Vec<&i32> = a_iter.collect();
println!("{:?}", b);

nth()

This method is similar to next(), ...