Updated code examples and wording

This commit is contained in:
erer1243
2021-06-05 23:44:21 -04:00
parent 6c2dd251bb
commit 67f4f3baec

View File

@@ -987,13 +987,13 @@ mod mod_keyword {}
/// Capture a [closure]'s environment by value. /// Capture a [closure]'s environment by value.
/// ///
/// `move` converts any variables captured by reference or mutable reference /// `move` converts any variables captured by reference or mutable reference
/// to owned by value variables. /// to variables captured by value.
/// ///
/// ```rust /// ```rust
/// let capture = "hello"; /// let data = vec![1, 2, 3];
/// let closure = move || { /// let closure = move || println!("captured {:?} by value", data);
/// println!("rust says {}", capture); ///
/// }; /// // data is no longer available, it is owned by the closure
/// ``` /// ```
/// ///
/// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though /// Note: `move` closures may still implement [`Fn`] or [`FnMut`], even though
@@ -1004,31 +1004,29 @@ mod mod_keyword {}
/// ```rust /// ```rust
/// fn create_fn() -> impl Fn() { /// fn create_fn() -> impl Fn() {
/// let text = "Fn".to_owned(); /// let text = "Fn".to_owned();
///
/// move || println!("This is a: {}", text) /// move || println!("This is a: {}", text)
/// } /// }
/// ///
/// let fn_plain = create_fn(); /// let fn_plain = create_fn();
///
/// fn_plain(); /// fn_plain();
/// ``` /// ```
/// ///
/// `move` is often used when [threads] are involved. /// `move` is often used when [threads] are involved.
/// ///
/// ```rust /// ```rust
/// let x = 5; /// let data = vec![1, 2, 3];
/// ///
/// std::thread::spawn(move || { /// std::thread::spawn(move || {
/// println!("captured {} by value", x) /// println!("captured {:?} by value", data)
/// }).join().unwrap(); /// }).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. /// `move` is also valid before an async block.
/// ///
/// ```rust /// ```rust
/// let capture = "hello"; /// let capture = "hello".to_owned();
/// let block = async move { /// let block = async move {
/// println!("rust says {} from async block", capture); /// println!("rust says {} from async block", capture);
/// }; /// };