Skip to content

Commit 1e55f58

Browse files
committed
Auto merge of rust-lang#73645 - poliorcetics:ref-keyword, r=jyn514
Document the ref keyword Partial fix for rust-lang#34601. This documents the `ref` keyword with two examples, one failing to compile because the `ref` keyword is missing, and the same example fixed with the keyword inserted in the correct place. It also explains (very *very* rapidly) the differences between `&` and `ref`. I put a link to the best place I could find in the Reference but there may be something better that I didn't find.
2 parents 5ef299e + 79f052b commit 1e55f58

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/libstd/keyword_docs.rs

+43-2
Original file line numberDiff line numberDiff line change
@@ -1060,9 +1060,50 @@ mod pub_keyword {}
10601060
//
10611061
/// Bind by reference during pattern matching.
10621062
///
1063-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1063+
/// `ref` annotates pattern bindings to make them borrow rather than move.
1064+
/// It is **not** a part of the pattern as far as matching is concerned: it does
1065+
/// not affect *whether* a value is matched, only *how* it is matched.
10641066
///
1065-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1067+
/// By default, [`match`] statements consume all they can, which can sometimes
1068+
/// be a problem, when you don't really need the value to be moved and owned:
1069+
///
1070+
/// ```compile_fail,E0382
1071+
/// let maybe_name = Some(String::from("Alice"));
1072+
/// // The variable 'maybe_name' is consumed here ...
1073+
/// match maybe_name {
1074+
/// Some(n) => println!("Hello, {}", n),
1075+
/// _ => println!("Hello, world"),
1076+
/// }
1077+
/// // ... and is now unavailable.
1078+
/// println!("Hello again, {}", maybe_name.unwrap_or("world".into()));
1079+
/// ```
1080+
///
1081+
/// Using the `ref` keyword, the value is only borrowed, not moved, making it
1082+
/// available for use after the [`match`] statement:
1083+
///
1084+
/// ```
1085+
/// let maybe_name = Some(String::from("Alice"));
1086+
/// // Using `ref`, the value is borrowed, not moved ...
1087+
/// match maybe_name {
1088+
/// Some(ref n) => println!("Hello, {}", n),
1089+
/// _ => println!("Hello, world"),
1090+
/// }
1091+
/// // ... so it's available here!
1092+
/// println!("Hello again, {}", maybe_name.unwrap_or("world".into()));
1093+
/// ```
1094+
///
1095+
/// # `&` vs `ref`
1096+
///
1097+
/// - `&` denotes that your pattern expects a reference to an object. Hence `&`
1098+
/// is a part of said pattern: `&Foo` matches different objects than `Foo` does.
1099+
///
1100+
/// - `ref` indicates that you want a reference to an unpacked value. It is not
1101+
/// matched against: `Foo(ref foo)` matches the same objects as `Foo(foo)`.
1102+
///
1103+
/// See also the [Reference] for more information.
1104+
///
1105+
/// [`match`]: keyword.match.html
1106+
/// [Reference]: ../reference/patterns.html#identifier-patterns
10661107
mod ref_keyword {}
10671108

10681109
#[doc(keyword = "return")]

0 commit comments

Comments
 (0)