Skip to content

Commit ceed619

Browse files
authored
Rollup merge of #86051 - erer1243:update_move_keyword_docs, r=Mark-Simulacrum
Updated code examples and wording in move keyword documentation Had a conversation with someone on the Rust Discord who was confused by the move keyword documentation. Some of the wording is odd sounding ("owned by value" - what else can something be owned by?). Also, some of the examples used Copy types when demonstrating move, leading to variables still being accessible in the outer scope after the move, contradicting the examples' comments. I changed the move keyword documentation a bit, removing that odd wording and changing all the examples to use non-Copy types
2 parents 232e7a5 + 67f4f3b commit ceed619

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

library/std/src/keyword_docs.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -987,13 +987,13 @@ mod mod_keyword {}
987987
/// Capture a [closure]'s environment by value.
988988
///
989989
/// `move` converts any variables captured by reference or mutable reference
990-
/// to owned by value variables.
990+
/// to variables captured by value.
991991
///
992992
/// ```rust
993-
/// let capture = "hello";
994-
/// let closure = move || {
995-
/// println!("rust says {}", capture);
996-
/// };
993+
/// let data = vec![1, 2, 3];
994+
/// let closure = move || println!("captured {:?} by value", data);
995+
///
996+
/// // data is no longer available, it is owned by the closure
997997
/// ```
998998
///
999999
/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though
@@ -1004,31 +1004,29 @@ mod mod_keyword {}
10041004
/// ```rust
10051005
/// fn create_fn() -> impl Fn() {
10061006
/// let text = "Fn".to_owned();
1007-
///
10081007
/// move || println!("This is a: {}", text)
10091008
/// }
10101009
///
10111010
/// let fn_plain = create_fn();
1012-
///
10131011
/// fn_plain();
10141012
/// ```
10151013
///
10161014
/// `move` is often used when [threads] are involved.
10171015
///
10181016
/// ```rust
1019-
/// let x = 5;
1017+
/// let data = vec![1, 2, 3];
10201018
///
10211019
/// std::thread::spawn(move || {
1022-
/// println!("captured {} by value", x)
1020+
/// println!("captured {:?} by value", data)
10231021
/// }).join().unwrap();
10241022
///
1025-
/// // x is no longer available
1023+
/// // data was moved to the spawned thread, so we cannot use it here
10261024
/// ```
10271025
///
10281026
/// `move` is also valid before an async block.
10291027
///
10301028
/// ```rust
1031-
/// let capture = "hello";
1029+
/// let capture = "hello".to_owned();
10321030
/// let block = async move {
10331031
/// println!("rust says {} from async block", capture);
10341032
/// };

0 commit comments

Comments
 (0)