Advanced Functions and Closures

Finally, we’ll explore some advanced features related to functions and closures, which include function pointers and returning closures.

Function Pointers

We’ve talked about how to pass closures to functions; you can also pass regular functions to functions! This technique is useful when you want to pass a function you’ve already defined rather than defining a new closure. Doing this with function pointers will allow you to use functions as arguments to other functions. Functions coerce to the type fn (with a lowercase f), not to be confused with the Fn closure trait. The fn type is called a function pointer. The syntax for specifying that a parameter is a function pointer is similar to that of closures, as shown in Listing 19-35.

Filename: src/main.rs

  1. fn add_one(x: i32) -> i32 {
  2. x + 1
  3. }
  4. fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
  5. f(arg) + f(arg)
  6. }
  7. fn main() {
  8. let answer = do_twice(add_one, 5);
  9. println!("The answer is: {}", answer);
  10. }

Listing 19-35: Using the fn type to accept a function pointer as an argument

This code prints The answer is: 12. We specify that the parameter f in do_twice is an fn that takes one parameter of type i32 and returns an i32. We can then call f in the body of do_twice. In main, we can pass the function name add_one as the first argument to do_twice.

Unlike closures, fn is a type rather than a trait, so we specify fn as the parameter type directly rather than declaring a generic type parameter with one of the Fn traits as a trait bound.

Function pointers implement all three of the closure traits (Fn, FnMut, and FnOnce), so you can always pass a function pointer as an argument for a function that expects a closure. It’s best to write functions using a generic type and one of the closure traits so your functions can accept either functions or closures.

An example of where you would want to only accept fn and not closures is when interfacing with external code that doesn’t have closures: C functions can accept functions as arguments, but C doesn’t have closures.

As an example of where you could use either a closure defined inline or a named function, let’s look at a use of map. To use the map function to turn a vector of numbers into a vector of strings, we could use a closure, like this:

  1. # #![allow(unused_variables)]
  2. #fn main() {
  3. let list_of_numbers = vec![1, 2, 3];
  4. let list_of_strings: Vec<String> = list_of_numbers
  5. .iter()
  6. .map(|i| i.to_string())
  7. .collect();
  8. #}

Or we could name a function as the argument to map instead of the closure, like this:

  1. # #![allow(unused_variables)]
  2. #fn main() {
  3. let list_of_numbers = vec![1, 2, 3];
  4. let list_of_strings: Vec<String> = list_of_numbers
  5. .iter()
  6. .map(ToString::to_string)
  7. .collect();
  8. #}

Note that we must use the fully qualified syntax that we talked about earlier in the “Advanced Traits” section because there are multiple functions available named to_string. Here, we’re using the to_string function defined in the ToString trait, which the standard library has implemented for any type that implements Display.

Some people prefer this style, and some people prefer to use closures. They end up compiling to the same code, so use whichever style is clearer to you.

Returning Closures

Closures are represented by traits, which means you can’t return closures directly. In most cases where you might want to return a trait, you can instead use the concrete type that implements the trait as the return value of the function. But you can’t do that with closures because they don’t have a concrete type that is returnable; you’re not allowed to use the function pointer fn as a return type, for example.

The following code tries to return a closure directly, but it won’t compile:

fn returns_closure() -> Fn(i32) -> i32 { |x| x + 1 }

The compiler error is as follows:

error[E0277]: the trait bound `std::ops::Fn(i32) -> i32 + 'static: std::marker::Sized` is not satisfied --> | 1 | fn returns_closure() -> Fn(i32) -> i32 { | ^^^^^^^^^^^^^^ `std::ops::Fn(i32) -> i32 + 'static` does not have a constant size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `std::ops::Fn(i32) -> i32 + 'static` = note: the return type of a function must have a statically known size

The error references the Sized trait again! Rust doesn’t know how much space it will need to store the closure. We saw a solution to this problem earlier. We can use a trait object:

  1. # #![allow(unused_variables)]
  2. #fn main() {
  3. fn returns_closure() -> Box<Fn(i32) -> i32> {
  4. Box::new(|x| x + 1)
  5. }
  6. #}

This code will compile just fine. For more about trait objects, refer to the “Using Trait Objects That Allow for Values of Different Types” section in Chapter 17.

Summary

Whew! Now you have some features of Rust in your toolbox that you won’t use often, but you’ll know they’re available in very particular circumstances. We’ve introduced several complex topics so that when you encounter them in error message suggestions or in other peoples’ code, you’ll be able to recognize these concepts and syntax. Use this chapter as a reference to guide you to solutions.

Next, we’ll put everything we’ve discussed throughout the book into practice and do one more project!