Skip to content

Commit 2f09deb

Browse files
authored
Rollup merge of rust-lang#66904 - DevinR528:keyword-doc, r=Dylan-DPC
Adding docs for keyword match, move Partial fix of issue rust-lang#34601.
2 parents e8b36c6 + e638f7c commit 2f09deb

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

Diff for: src/libstd/keyword_docs.rs

+71-5
Original file line numberDiff line numberDiff line change
@@ -812,9 +812,50 @@ mod loop_keyword { }
812812
//
813813
/// Control flow based on pattern matching.
814814
///
815-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
815+
/// `match` can be used to run code conditionally. Every pattern must
816+
/// be handled exhaustively either explicitly or by using wildcards like
817+
/// `_` in the `match`. Since `match` is an expression, values can also be
818+
/// returned.
816819
///
817-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
820+
/// ```rust
821+
/// let opt = Option::None::<usize>;
822+
/// let x = match opt {
823+
/// Some(int) => int,
824+
/// None => 10,
825+
/// };
826+
/// assert_eq!(x, 10);
827+
///
828+
/// let a_number = Option::Some(10);
829+
/// match a_number {
830+
/// Some(x) if x <= 5 => println!("0 to 5 num = {}", x),
831+
/// Some(x @ 6..=10) => println!("6 to 10 num = {}", x),
832+
/// None => panic!(),
833+
/// // all other numbers
834+
/// _ => panic!(),
835+
/// }
836+
/// ```
837+
///
838+
/// `match` can be used to gain access to the inner members of an enum
839+
/// and use them directly.
840+
///
841+
/// ```rust
842+
/// enum Outer {
843+
/// Double(Option<u8>, Option<String>),
844+
/// Single(Option<u8>),
845+
/// Empty
846+
/// }
847+
///
848+
/// let get_inner = Outer::Double(None, Some(String::new()));
849+
/// match get_inner {
850+
/// Outer::Double(None, Some(st)) => println!("{}", st),
851+
/// Outer::Single(opt) => println!("{:?}", opt),
852+
/// _ => panic!(),
853+
/// }
854+
/// ```
855+
///
856+
/// For more information on `match` and matching in general, see the [Reference].
857+
///
858+
/// [Reference]: ../reference/expressions/match-expr.html
818859
mod match_keyword { }
819860

820861
#[doc(keyword = "mod")]
@@ -831,10 +872,35 @@ mod mod_keyword { }
831872
//
832873
/// Capture a [closure]'s environment by value.
833874
///
834-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
875+
/// `move` converts any variables captured by reference or mutable reference
876+
/// to owned by value variables. The three [`Fn` trait]'s mirror the ways to capture
877+
/// variables, when `move` is used, the closures is represented by the `FnOnce` trait.
835878
///
836-
/// [closure]: ../book/second-edition/ch13-01-closures.html
837-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
879+
/// ```rust
880+
/// let capture = "hello";
881+
/// let closure = move || {
882+
/// println!("rust says {}", capture);
883+
/// };
884+
/// ```
885+
///
886+
/// `move` is often used when [threads] are involved.
887+
///
888+
/// ```rust
889+
/// let x = 5;
890+
///
891+
/// std::thread::spawn(move || {
892+
/// println!("captured {} by value", x)
893+
/// }).join().unwrap();
894+
///
895+
/// // x is no longer available
896+
/// ```
897+
///
898+
/// For more information on the `move` keyword, see the [closure]'s section
899+
/// of the Rust book or the [threads] section
900+
///
901+
/// [`Fn` trait]: ../std/ops/trait.Fn.html
902+
/// [closure]: ../book/ch13-01-closures.html
903+
/// [threads]: ../book/ch16-01-threads.html#using-move-closures-with-threads
838904
mod move_keyword { }
839905

840906
#[doc(keyword = "mut")]

0 commit comments

Comments
 (0)