Book · Part 1 · Chapter 9

Closures and function values

A closure carries the values it captured. If one of them is linear, the closure is too.

use std::io::print;

fn double(x: u32) -> u32 { x * 2 }

// A plain function value.
fn apply_fn(f: fn(u32) -> u32, x: u32) -> u32 {
    f(x)
}

// A closure parameter called once: its environment is a hidden type parameter.
fn apply_once(f: closure(u32) -> u32, x: u32) -> u32 {
    f(x)
}

// Called twice, so the environment must be Copy; it is named to say so.
forall Env where Copy[Env].
fn apply_twice(f: closure[Env](u32) -> u32, x: u32) -> u32 {
    f(f(x))
}

fn main() {
    let offset = 10u32;
    let add = |x: u32| x + offset;          // captures `offset` by copy
    let a = apply_fn(double, 4);            // a named fn as a value
    let g = double;
    let b = g(5);
    let c = apply_once(add, 1);
    let d = apply_twice(|x| x + offset, 1);
    let e = apply_twice(|x| -> u32 {
        let y = x * 3;
        y + 1
    }, 2);
    print(f"{a} {b} {c} {d} {e}\n");
}
8 10 11 21 22

Writing a closure

  • |x| x + 1 and |x: u32| x + 1
  • || 42, with no parameters
  • |x| -> u32 { let y = x * 3; y + 1 }, with a block body and a result type

A closure captures the local variables it mentions. A value that is Copy is captured by copy, and a linear value is moved in. Moving a linear value into a closure makes the closure itself linear, and it then has to be called, or handed on, exactly once.

A closure holds its own value of what it captures, a copy or the value moved in, so it cannot change the original. Assigning to a captured name, or lending it, is rejected, even when the name is a var:

fn main() {
    var total = 0u32;
    let add = |x: u32| { total = total + x; };
    add(1);
}
lib.kur:3:26: total is captured, and a body cannot assign what it captures; assign it outside, or give a closure a place parameter for it
  3 |     let add = |x: u32| { total = total + x; };
    |                          ^

Lending it, as in || &c.tick() with the Counter of chapter 5, gives c is captured, and a body cannot lend what it captures; lend it outside, or give a closure a place parameter for it. Make the change outside the closure, have the closure return the new value, or give the closure a place parameter (chapter 12).

Two kinds of function value

A function value is written fn(A) -> R. A named function used without parentheses is one, as in apply_fn(double, 4) and let g = double;. It carries nothing with it.

A closure carries an environment, the values it captured. Its type is closure[Env](A) -> R, where Env is a type parameter for that environment. Two closures with the same signature but different captures have different Env.

As a parameter, you can leave Env unnamed and write closure(A) -> R, as apply_once does. The environment is then a hidden type parameter, and this form is only allowed for parameters. When a function needs to say something about the environment, it names it, as apply_twice does with where Copy[Env].

A closure parameter does not accept a plain function value. To pass double to apply_once, wrap it: apply_once(|x| double(x), 4).

Calling more than once

Calling a closure uses it up unless its environment is Copy. That follows from the rule for linear values. A closure that captured a linear ticket is linear:

let t = issue(4);
let r = apply_once(|x| x + redeem(t), 1);   // t moves into the closure, which is called once

Call it twice and the second call fails:

(after the three one-line Ticket! declarations from chapter 6)

fn main() {
    let t = issue(4);
    let f = |x: u32| x + redeem(t);
    let a = f(1);
    let b = f(2);
}
lib.kur:8:13: demo::main: value 'f' has been moved or is only partially available
  8 |     let b = f(2);
    |             ^

Handing that closure to apply_twice fails at the call, with type argument must satisfy Copy. Inside a function, a closure(...) parameter that is called twice is rejected for the same reason, since nothing says its environment is Copy:

fn apply_twice(f: closure(u32) -> u32, x: u32) -> u32 {
    f(f(x))
}
lib.kur:2:7: demo::apply_twice: value 'f' has been moved or is only partially available
  2 |     f(f(x))
    |       ^

The fix is to name the environment and say it is Copy, as in the program at the top.

What is not here

This chapter covers closures as arguments. Returning a closure from a function is not covered yet, because the way to write its type is still being settled.

Check by hand

For a closure, list what it mentions from outside. If any of it is linear, the closure is linear and is called once or handed on. A function that calls its closure parameter more than once says Copy[Env].

Next: for and f-strings →