-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Documentation for the 'break' keyword in the std library #64270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,55 @@ mod as_keyword { } | |
#[doc(keyword = "break")] | ||
// | ||
/// Exit early from a loop. | ||
/// | ||
/// Sometimes, during the execution of a `loop`, `for` or `while` loop it is desirable to end the | ||
/// loop without preforming any more iterations. This is common in scenarios where additional | ||
/// iterations would be redundant, such as linear searches, or when an exit condition must be | ||
/// checked in the middle or at the end of an iteration (similar to a C-style do-while block). | ||
/// | ||
/// ```rust | ||
/// let numbers: Vec<i32> = vec![-2, 9, 4, 6]; | ||
/// for x in numbers.iter() { | ||
/// if x == &4 { | ||
/// println!("Contains 4!"); | ||
/// break; | ||
/// } | ||
/// println!("Not 4. Found {}", x); | ||
/// } | ||
/// ``` | ||
/// | ||
/// The documentation for this keyword is [not yet complete]. Pull requests welcome! | ||
/// By default, `break` will only end execution of the innermost loop. However, in applications | ||
/// with nested loops, labels can be used to specify which loop should be terminated. | ||
/// | ||
/// ```rust | ||
/// 'outer: loop { | ||
/// println!("In the outer loop!"); | ||
/// 'inner: loop { | ||
/// println!("In the inner loop!"); | ||
/// break 'outer; | ||
/// } | ||
/// unreachable!(); | ||
/// } | ||
/// ``` | ||
/// | ||
/// `break` can also be to return a value from a `loop`, `for` or `while` block. To ensure type | ||
/// safety, all returned values must be of the same type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add something like “If |
||
/// | ||
/// ```rust | ||
/// let mut x = 3; | ||
/// let result = loop { | ||
/// if x > 11 { | ||
/// break x; | ||
/// } | ||
/// x = x * x; | ||
/// }; | ||
/// assert_eq!(81, result); | ||
/// ``` | ||
/// | ||
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601 | ||
/// For more detail on `break`, see the [Rust Book] or the [Reference]. | ||
/// | ||
/// [Rust Book]: ../book/ch03-05-control-flow.html?#repetition-with-loops | ||
/// [Reference]: ../reference/expressions/loop-expr.html | ||
mod break_keyword { } | ||
|
||
#[doc(keyword = "const")] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
“can also be used to return a value”?