@@ -608,6 +608,62 @@ mod in_keyword { }
608
608
/// [Reference]: ../reference/statements.html#let-statements
609
609
mod let_keyword { }
610
610
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
+
611
667
#[ doc( keyword = "loop" ) ]
612
668
//
613
669
/// Loop indefinitely.
@@ -922,15 +978,6 @@ mod use_keyword { }
922
978
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
923
979
mod where_keyword { }
924
980
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
-
934
981
// 2018 Edition keywords
935
982
936
983
#[ unstable( feature = "async_await" , issue = "50547" ) ]
0 commit comments