@@ -33,9 +33,71 @@ mod as_keyword { }
3333//
3434/// Exit early from a loop.
3535///
36- /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
36+ /// When `break` is encountered, execution of the associated loop body is
37+ /// immediately terminated.
38+ ///
39+ /// ```rust
40+ /// let mut last = 0;
41+ ///
42+ /// for x in 1..100 {
43+ /// if x > 12 {
44+ /// break;
45+ /// }
46+ /// last = x;
47+ /// }
48+ ///
49+ /// assert_eq!(last, 12);
50+ /// println!("{}", last);
51+ /// ```
52+ ///
53+ /// A break expression is normally associated with the innermost loop enclosing the
54+ /// `break` but a label can be used to specify which enclosing loop is affected.
55+ ///
56+ ///```rust
57+ /// 'outer: for i in 1..=5 {
58+ /// println!("outer iteration (i): {}", i);
59+ ///
60+ /// 'inner: for j in 1..=200 {
61+ /// println!(" inner iteration (j): {}", j);
62+ /// if j >= 3 {
63+ /// // breaks from inner loop, let's outer loop continue.
64+ /// break;
65+ /// }
66+ /// if i >= 2 {
67+ /// // breaks from outer loop, and directly to "Bye".
68+ /// break 'outer;
69+ /// }
70+ /// }
71+ /// }
72+ /// println!("Bye.");
73+ ///```
74+ ///
75+ /// When associated with `loop`, but not with any other kind of loop expression,
76+ /// `break` can return a value. When no value is specified, `break;` returns `()`.
77+ /// Every `break` within a loop must return the same type.
78+ ///
79+ /// ```rust
80+ /// let (mut a, mut b) = (1, 1);
81+ /// let result = loop {
82+ /// if b > 10 {
83+ /// break b;
84+ /// }
85+ /// let c = a + b;
86+ /// a = b;
87+ /// b = c;
88+ /// };
89+ /// // first number in Fibonacci sequence over 10:
90+ /// assert_eq!(result, 13);
91+ /// println!("{}", result);
92+ /// ```
93+ ///
94+ /// For more details consult the [Reference on "break expression"] and the [Reference on "break and
95+ /// loop values"].
96+ ///
97+ /// [Reference on "break expression"]: ../reference/expressions/loop-expr.html#break-expressions
98+ /// [Reference on "break and loop values"]:
99+ /// ../reference/expressions/loop-expr.html#break-and-loop-values
37100///
38- /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
39101mod break_keyword { }
40102
41103#[ doc( keyword = "const" ) ]
0 commit comments