about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorDevin R <devin.ragotzy@gmail.com>2019-11-30 08:54:39 -0500
committerDevin R <devin.ragotzy@gmail.com>2019-11-30 08:54:39 -0500
commit71abce1e5d99f37d52bd9b3943db980119ff286b (patch)
tree765b4e0b577a6ab6bebdb8715105b8814262ee3c /src/libstd
parent8f1bbd69e13c9e04a4c2b75612bc0c31af972439 (diff)
downloadrust-71abce1e5d99f37d52bd9b3943db980119ff286b.tar.gz
rust-71abce1e5d99f37d52bd9b3943db980119ff286b.zip
document match and move keywords
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/keyword_docs.rs69
1 files changed, 64 insertions, 5 deletions
diff --git a/src/libstd/keyword_docs.rs b/src/libstd/keyword_docs.rs
index b0baf36308e..6bc0af47be5 100644
--- a/src/libstd/keyword_docs.rs
+++ b/src/libstd/keyword_docs.rs
@@ -812,9 +812,48 @@ mod loop_keyword { }
 //
 /// Control flow based on pattern matching.
 ///
-/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
-///
-/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
+/// `match` can be used to run code conditionally. Every pattern must
+/// be handled exhaustively either explicitly or by using wildcards like
+/// `_` in the `match`. Since `match` is an expression values can also be
+/// returned.
+/// 
+/// ```rust
+/// let opt = Option::None::<usize>;
+/// let x = match opt {
+///     Some(int) => int,
+///     None => 10,
+/// }
+/// assert_eq!(x, 10);
+/// 
+/// let a_number = Option::Some(10);
+/// match a_number {
+///     Some(x) if x <= 5 => println!("0 to 5 num = {}", x),
+///     Some(x @ 6..=10) => println!("6 to 10 num = {}", x),
+///     None => oh_no(),
+///     _ => all_other_numbers(),
+/// }
+/// ```
+/// 
+/// `match` can be used to gain access to the inner members of an enum 
+/// and use them directly.
+/// 
+/// ```rust
+/// enum Outer {
+///     Double(Option<u8>, Option<String>),
+///     Single(Option<u8>),
+///     Empty
+/// }
+/// 
+/// let get_inner = Outer::Double(None, Some(String::new()));
+/// match get_inner {
+///     Outer::Double(None, Some(st)) => println!("{}", st),
+///     Outer::Single(opt) => println!("{:?}", opt),
+///     _ => the_rest(),
+/// }
+/// ```
+/// 
+/// For more information on `match` and matching in general, see the [Reference].
+/// [Reference]: ../reference/expressions/match-expr.html
 mod match_keyword { }
 
 #[doc(keyword = "mod")]
@@ -831,9 +870,29 @@ mod mod_keyword { }
 //
 /// Capture a [closure]'s environment by value.
 ///
-/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
+/// `move` converts any variables captured by reference or mutable reference
+/// to owned by value variables. The three [`Fn` trait]'s mirror the ways to capture
+/// variables, when `move` is used the closures is represented by the `FnOnce` trait.
+/// 
+/// ```rust
+/// 
+/// ```
+/// 
+/// `move` is often used when [threads] are involved.
+/// 
+/// ```rust
+/// let x = 5;
+/// 
+/// std::thread::spawn(move || {
+///     println!("captured {} by value", x)
+/// }).join().unwrap();
+/// 
+/// // x is no longer available
+/// ```
 ///
-/// [closure]: ../book/second-edition/ch13-01-closures.html
+/// [`Fn` trait]: ../std/ops/trait.Fn.html
+/// [closure]: ../book/ch13-01-closures.html
+/// [threads]: ../book/ch16-01-threads.html#using-move-closures-with-threads
 /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
 mod move_keyword { }