Skip to content

Commit 60f300a

Browse files
authored
Rollup merge of rust-lang#62462 - JohnTitor:document-while, r=Centril
Document `while` keyword This is a rework of rust-lang#60761. Closes rust-lang#60736 r? @dtolnay
2 parents 6da3098 + 559c3ec commit 60f300a

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

src/libstd/keyword_docs.rs

+56-9
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,62 @@ mod in_keyword { }
608608
/// [Reference]: ../reference/statements.html#let-statements
609609
mod let_keyword { }
610610

611+
#[doc(keyword = "while")]
612+
//
613+
/// Loop while a condition is upheld.
614+
///
615+
/// A `while` expression is used for predicate loops. The `while` expression runs the conditional
616+
/// expression before running the loop body, then runs the loop body if the conditional
617+
/// expression evaluates to `true`, or exits the loop otherwise.
618+
///
619+
/// ```rust
620+
/// let mut counter = 0;
621+
///
622+
/// while counter < 10 {
623+
/// println!("{}", counter);
624+
/// counter += 1;
625+
/// }
626+
/// ```
627+
///
628+
/// Like the [`for`] expression, we can use `break` and `continue`. A `while` expression
629+
/// cannot break with a value and always evaluates to `()` unlike [`loop`].
630+
///
631+
/// ```rust
632+
/// let mut i = 1;
633+
///
634+
/// while i < 100 {
635+
/// i *= 2;
636+
/// if i == 64 {
637+
/// break; // Exit when `i` is 64.
638+
/// }
639+
/// }
640+
/// ```
641+
///
642+
/// As `if` expressions have their pattern matching variant in `if let`, so too do `while`
643+
/// expressions with `while let`. The `while let` expression matches the pattern against the
644+
/// expression, then runs the loop body if pattern matching succeeds, or exits the loop otherwise.
645+
/// We can use `break` and `continue` in `while let` expressions just like in `while`.
646+
///
647+
/// ```rust
648+
/// let mut counter = Some(0);
649+
///
650+
/// while let Some(i) = counter {
651+
/// if i == 10 {
652+
/// counter = None;
653+
/// } else {
654+
/// println!("{}", i);
655+
/// counter = Some (i + 1);
656+
/// }
657+
/// }
658+
/// ```
659+
///
660+
/// For more information on `while` and loops in general, see the [reference].
661+
///
662+
/// [`for`]: keyword.for.html
663+
/// [`loop`]: keyword.loop.html
664+
/// [reference]: ../reference/expressions/loop-expr.html#predicate-loops
665+
mod while_keyword { }
666+
611667
#[doc(keyword = "loop")]
612668
//
613669
/// Loop indefinitely.
@@ -922,15 +978,6 @@ mod use_keyword { }
922978
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
923979
mod where_keyword { }
924980

925-
#[doc(keyword = "while")]
926-
//
927-
/// Loop while a condition is upheld.
928-
///
929-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
930-
///
931-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
932-
mod while_keyword { }
933-
934981
// 2018 Edition keywords
935982

936983
#[unstable(feature = "async_await", issue = "50547")]

0 commit comments

Comments
 (0)