Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the ref keyword #73645

Merged
merged 3 commits into from
Jul 25, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/libstd/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,50 @@ mod pub_keyword {}
//
/// Bind by reference during pattern matching.
///
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
/// `ref` annotates pattern bindings to make them borrow rather than move.
/// It is **not** a part of the pattern as far as matching is concerned: it does
/// not affect *whether* a value is matched, only *how* it is matched.
///
/// By default, [`match`] statements consume all they can, which can sometimes
/// be a problem, when you don't really need the value to be moved and owned:
///
/// ```compile_fail,E0382
/// let maybe_name = Some(String::from("Alice"));
/// // The variable 'maybe_name' is consumed here ...
/// match maybe_name {
/// Some(n) => println!("Hello, {}", n),
/// _ => println!("Hello, world"),
/// }
/// // ... and now unavailable.
poliorcetics marked this conversation as resolved.
Show resolved Hide resolved
/// println!("Hello again, {}", maybe_name.unwrap_or("world".into()));
/// ```
///
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
/// Using the `ref` keyword, the value is only borrowed, not moved, making
/// available for use after the [`match`] statement:
poliorcetics marked this conversation as resolved.
Show resolved Hide resolved
poliorcetics marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```
/// let maybe_name = Some(String::from("Alice"));
/// // Using `ref`, the value is borrowed, not moved ...
/// match maybe_name {
/// Some(ref n) => println!("Hello, {}", n),
/// _ => println!("Hello, world"),
/// }
/// // ... and it's available here !
poliorcetics marked this conversation as resolved.
Show resolved Hide resolved
/// println!("Hello again, {}", maybe_name.unwrap_or("world".into()));
/// ```
///
/// # `&` vs `ref`
///
/// - `&` denotes that your pattern expects a reference to an object. Hence `&`
/// is a part of said pattern: `&Foo` matches different objects than `Foo` does.
///
/// - `ref` indicates that you want a reference to an unpacked value. It is not
/// matched against: `Foo(ref foo)` matches the same objects as `Foo(foo)`.
///
/// See also the [Reference] for more information.
///
/// [`match`]: keyword.match.html
/// [Reference]: ../reference/patterns.html#identifier-patterns
mod ref_keyword {}

#[doc(keyword = "return")]
Expand Down