Closures with explicit lifetimes – a special case

As we saw back in Chapter 8, The Rust Application Lifetime, there are two main types of scope: global and local. A variable that has a local scope goes out of bounds as soon as it is finished with, whereas a global scope variable is cleaned up when the application terminates. A global scope variable is also given the lifetime marker, '.

Closures also have different scopes. Typically, they will only be for the lifetime if they are called in, but they can also be global.

A "normal" function (as shown previously) would be as follows:

fn call_with_three<F>(some_closure: F) -> i32 where F : Fn(i32) -> i32
{
some_closure(3)
}

Conversely, for a lifetime scope, we would have the following:

fn call_with_three<'a, F>(some_closure: F) -> i32 where F : Fn(&'a 32) -> i32

However, this won't compile. The problem is with the scope.

In our first example, the scope is purely for the lifetime of the invocation. In the second, it's for the lifetime of the function (and that is for the entirety of the function), which means that the compiler will see a mutable reference at the same lifetime as the immutable reference.

Although Rust does still allow us to use this, we need to use something called a higher-ranked trait bounds (in simple terms, it means that, in order of importance, this trumps something below it). This works by telling the compiler to use the minimum lifetime for the closure to run, which in turn should satisfy the borrow-checker. In this case, we use for<...>:

fn call_with_three<'a, F>(some_closure: F) -> i32 where F :<for 'a> Fn(&'a 32) -> i32
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset