Skip to content

Commit 76a353b

Browse files
committed
Add keyword docs for loop.
1 parent 165690b commit 76a353b

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/libstd/keyword_docs.rs

+45
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,51 @@ mod impl_keyword { }
576576
/// [Reference]: https://doc.rust-lang.org/reference/statements.html#let-statements
577577
mod let_keyword { }
578578

579+
#[doc(keyword = "loop")]
580+
//
581+
/// The loop-defining keyword.
582+
///
583+
/// `loop` is used to define the simplest kind of loop supported in Rust. It runs the code inside
584+
/// it until the code uses `break` or the program exits.
585+
///
586+
/// ```rust
587+
/// loop {
588+
/// println!("hello world forever!");
589+
/// # break;
590+
/// }
591+
///
592+
/// let mut i = 0;
593+
/// loop {
594+
/// println!("i is {}", i);
595+
/// if i > 10 {
596+
/// break;
597+
/// }
598+
/// i += 1;
599+
/// }
600+
/// ```
601+
///
602+
/// Unlike the other kinds of loops in Rust (`while`, `while let`, and `for`), loops can be used as
603+
/// expressions that return values via `break`.
604+
///
605+
/// ```rust
606+
/// let mut i = 1;
607+
/// let something = loop {
608+
/// i *= 2;
609+
/// if i > 100 {
610+
/// break i;
611+
/// }
612+
/// };
613+
/// assert_eq!(something, 128);
614+
/// ```
615+
///
616+
/// Every `break` in a loop has to have the same type. When it's not explicitly giving something,
617+
/// `break;` returns `()`.
618+
///
619+
/// For more information on `loop` and loops in general, see the [Reference].
620+
///
621+
/// [Reference]: https://doc.rust-lang.org/reference/expressions/loop-expr.html
622+
mod loop_keyword { }
623+
579624
#[doc(keyword = "struct")]
580625
//
581626
/// The keyword used to define structs.

0 commit comments

Comments
 (0)