Skip to content

Commit 1cd8dff

Browse files
committed
Add an example about the behaviour of move and Fn* traits
1 parent d243fa1 commit 1cd8dff

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

library/std/src/keyword_docs.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -945,18 +945,30 @@ mod mod_keyword {}
945945
/// `move` converts any variables captured by reference or mutable reference
946946
/// to owned by value variables.
947947
///
948-
/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though
949-
/// they capture variables by `move`. This is because the traits implemented by
950-
/// a closure type are determined by *what* the closure does with captured
951-
/// values, not *how* it captures them.
952-
///
953948
/// ```rust
954949
/// let capture = "hello";
955950
/// let closure = move || {
956951
/// println!("rust says {}", capture);
957952
/// };
958953
/// ```
959954
///
955+
/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though
956+
/// they capture variables by `move`. This is because the traits implemented by
957+
/// a closure type are determined by *what* the closure does with captured
958+
/// values, not *how* it captures them:
959+
///
960+
/// ```rust
961+
/// fn create_fn() -> impl Fn() {
962+
/// let text = "Fn".to_owned();
963+
///
964+
/// move || println!("This is a: {}", text)
965+
/// }
966+
///
967+
/// let fn_plain = create_fn();
968+
///
969+
/// fn_plain();
970+
/// ```
971+
///
960972
/// `move` is often used when [threads] are involved.
961973
///
962974
/// ```rust

0 commit comments

Comments
 (0)