]> git.lizzy.rs Git - rust.git/commitdiff
Add an example about the behaviour of move and Fn* traits
authorAlexis Bourget <alexis.bourget@gmail.com>
Sat, 8 Aug 2020 13:57:17 +0000 (15:57 +0200)
committerAlexis Bourget <alexis.bourget@gmail.com>
Sat, 8 Aug 2020 13:57:17 +0000 (15:57 +0200)
library/std/src/keyword_docs.rs

index 93776328290d3d093067a2a7af9e959e4867795a..ff343625a19ed75932bf944f9e9cd57f394aa1e1 100644 (file)
@@ -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