diff options
| author | bors <bors@rust-lang.org> | 2019-10-21 15:50:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-10-21 15:50:37 +0000 |
| commit | 10f12fe3e73f3b6f7e6d6f8bbd87b1a8b4e74a07 (patch) | |
| tree | 9260a62704956f17a2d67fb4ab7cc6faf92ca846 /src/libstd | |
| parent | b7a9c285a50f3a94c44687ba9ff3ab0648243aaa (diff) | |
| parent | 1c94a4475bb3ef7e13eb59dc6fd0920051544b45 (diff) | |
| download | rust-10f12fe3e73f3b6f7e6d6f8bbd87b1a8b4e74a07.tar.gz rust-10f12fe3e73f3b6f7e6d6f8bbd87b1a8b4e74a07.zip | |
Auto merge of #65661 - JohnTitor:rollup-68la1fq, r=JohnTitor
Rollup of 5 pull requests Successful merges: - #65544 (Added doc on keyword break) - #65620 (Correctly note code as Ok not error for E0573) - #65624 ([mir-opt] Improve SimplifyLocals pass so it can remove unused consts) - #65650 (use unwrap_or in lint code) - #65652 (Fix `canonicalize_const_var` leaking inference variables) Failed merges: r? @ghost
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/keyword_docs.rs | 67 |
1 files changed, 65 insertions, 2 deletions
diff --git a/src/libstd/keyword_docs.rs b/src/libstd/keyword_docs.rs index a8dfe924fdf..ab8a55660cb 100644 --- a/src/libstd/keyword_docs.rs +++ b/src/libstd/keyword_docs.rs @@ -33,9 +33,72 @@ mod as_keyword { } // /// Exit early from a loop. /// -/// The documentation for this keyword is [not yet complete]. Pull requests welcome! +/// When `break` is encountered, execution of the associated loop body is +/// immediately terminated. +/// +/// ```rust +/// let mut last = 0; +/// +/// for x in 1..100 { +/// if x > 12 { +/// break; +/// } +/// last = x; +/// } +/// +/// assert_eq!(last, 12); +/// println!("{}", last); +/// ``` +/// +/// A break expression is normally associated with the innermost loop enclosing the +/// `break` but a label can be used to specify which enclosing loop is affected. +/// +///```rust +/// 'outer: for i in 1..=5 { +/// println!("outer iteration (i): {}", i); +/// +/// 'inner: for j in 1..=200 { +/// println!(" inner iteration (j): {}", j); +/// if j >= 3 { +/// // breaks from inner loop, let's outer loop continue. +/// break; +/// } +/// if i >= 2 { +/// // breaks from outer loop, and directly to "Bye". +/// break 'outer; +/// } +/// } +/// } +/// println!("Bye."); +///``` +/// +/// When associated with `loop`, a break expression may be used to return a value from that loop. +/// This is only valid with `loop` and not with any other type of loop. +/// If no value is specified, `break;` returns `()`. +/// Every `break` within a loop must return the same type. +/// +/// ```rust +/// let (mut a, mut b) = (1, 1); +/// let result = loop { +/// if b > 10 { +/// break b; +/// } +/// let c = a + b; +/// a = b; +/// b = c; +/// }; +/// // first number in Fibonacci sequence over 10: +/// assert_eq!(result, 13); +/// println!("{}", result); +/// ``` +/// +/// For more details consult the [Reference on "break expression"] and the [Reference on "break and +/// loop values"]. +/// +/// [Reference on "break expression"]: ../reference/expressions/loop-expr.html#break-expressions +/// [Reference on "break and loop values"]: +/// ../reference/expressions/loop-expr.html#break-and-loop-values /// -/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601 mod break_keyword { } #[doc(keyword = "const")] |
