From d243fa109fd22d334a717e4f03bf208ce9c3a9f4 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Tue, 4 Aug 2020 22:35:06 +0200 Subject: [PATCH 1/2] Fix the documentation for move about Fn traits implementations --- library/std/src/keyword_docs.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/std/src/keyword_docs.rs b/library/std/src/keyword_docs.rs index c98008688ab..93776328290 100644 --- a/library/std/src/keyword_docs.rs +++ b/library/std/src/keyword_docs.rs @@ -943,8 +943,12 @@ mod mod_keyword {} /// Capture a [closure]'s environment by value. /// /// `move` converts any variables captured by reference or mutable reference -/// to owned by value variables. The three [`Fn` trait]'s mirror the ways to capture -/// variables, when `move` is used, the closures is represented by the `FnOnce` trait. +/// to owned by value variables. +/// +/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though +/// they capture variables by `move`. This is because the traits implemented by +/// a closure type are determined by *what* the closure does with captured +/// values, not *how* it captures them. /// /// ```rust /// let capture = "hello"; From 1cd8dffdae5826931c59329c168ee956800ee6df Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sat, 8 Aug 2020 15:57:17 +0200 Subject: [PATCH 2/2] Add an example about the behaviour of move and Fn* traits --- library/std/src/keyword_docs.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/library/std/src/keyword_docs.rs b/library/std/src/keyword_docs.rs index 93776328290..ff343625a19 100644 --- a/library/std/src/keyword_docs.rs +++ b/library/std/src/keyword_docs.rs @@ -945,11 +945,6 @@ mod mod_keyword {} /// `move` converts any variables captured by reference or mutable reference /// to owned by value variables. /// -/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though -/// they capture variables by `move`. This is because the traits implemented by -/// a closure type are determined by *what* the closure does with captured -/// values, not *how* it captures them. -/// /// ```rust /// let capture = "hello"; /// let closure = move || { @@ -957,6 +952,23 @@ mod mod_keyword {} /// }; /// ``` /// +/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though +/// they capture variables by `move`. This is because the traits implemented by +/// a closure type are determined by *what* the closure does with captured +/// values, not *how* it captures them: +/// +/// ```rust +/// fn create_fn() -> impl Fn() { +/// let text = "Fn".to_owned(); +/// +/// move || println!("This is a: {}", text) +/// } +/// +/// let fn_plain = create_fn(); +/// +/// fn_plain(); +/// ``` +/// /// `move` is often used when [threads] are involved. /// /// ```rust