From 5cbb8d4c27510065d2048784adefa97daa65bc7f Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 16 Jan 2023 15:35:10 +0000 Subject: [PATCH] Add speaker notes about closures. --- src/generics/closures.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/generics/closures.md b/src/generics/closures.md index 248aa405..83b2000a 100644 --- a/src/generics/closures.md +++ b/src/generics/closures.md @@ -19,3 +19,20 @@ fn main() { println!("mul_5: {}", apply_with_log(mul_5, 20)); } ``` + +
+ +If you have an `FnOnce`, you may only call it once. It might consume captured values. + +An `FnMut` might mutate captured values, so you can call it multiple times but not concurrently. + +An `Fn` neither consumes nor mutates captured values, or perhaps captures nothing at all, so it can +be called multiple times concurrently. + +`FnMut` is a subtype of `FnOnce`. `Fn` is a subtype of `FnMut` and `FnOnce`. I.e. you can use an +`FnMut` wherever an `FnOnce` is called for, and you can use an `Fn` wherever an `FnMut` or `FnOnce` +is called for. + +`move` closures only implement `FnOnce`. + +