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
This commit is contained in:
commit
ceed619194
1 changed files with 9 additions and 11 deletions
|
@ -987,13 +987,13 @@ 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.
|
||||
/// to variables captured by value.
|
||||
///
|
||||
/// ```rust
|
||||
/// let capture = "hello";
|
||||
/// let closure = move || {
|
||||
/// println!("rust says {}", capture);
|
||||
/// };
|
||||
/// let data = vec![1, 2, 3];
|
||||
/// let closure = move || println!("captured {:?} by value", data);
|
||||
///
|
||||
/// // data is no longer available, it is owned by the closure
|
||||
/// ```
|
||||
///
|
||||
/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though
|
||||
|
@ -1004,31 +1004,29 @@ mod mod_keyword {}
|
|||
/// ```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
|
||||
/// let x = 5;
|
||||
/// let data = vec![1, 2, 3];
|
||||
///
|
||||
/// std::thread::spawn(move || {
|
||||
/// println!("captured {} by value", x)
|
||||
/// println!("captured {:?} by value", data)
|
||||
/// }).join().unwrap();
|
||||
///
|
||||
/// // x is no longer available
|
||||
/// // data was moved to the spawned thread, so we cannot use it here
|
||||
/// ```
|
||||
///
|
||||
/// `move` is also valid before an async block.
|
||||
///
|
||||
/// ```rust
|
||||
/// let capture = "hello";
|
||||
/// let capture = "hello".to_owned();
|
||||
/// let block = async move {
|
||||
/// println!("rust says {} from async block", capture);
|
||||
/// };
|
||||
|
|
Loading…
Add table
Reference in a new issue