-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Document become
keyword
#113095
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
Open
WaffleLapkin
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
WaffleLapkin:document_becoming_unuwuable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Document become
keyword
#113095
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 | ||||
---|---|---|---|---|---|---|
|
@@ -1228,6 +1228,69 @@ mod ref_keyword {} | |||||
/// ``` | ||||||
mod return_keyword {} | ||||||
|
||||||
#[doc(keyword = "become")] | ||||||
// | ||||||
/// Perform a tail-call of a function. | ||||||
/// | ||||||
/// A `become` transfers the execution flow to a function in such a way, that | ||||||
/// returning from the callee returns to the caller of the current function: | ||||||
/// | ||||||
/// ``` | ||||||
/// #![feature(explicit_tail_calls)] | ||||||
/// | ||||||
/// fn a() -> u32 { | ||||||
/// become b(); | ||||||
/// } | ||||||
/// | ||||||
/// fn b() -> u32 { | ||||||
/// return 2; // this return directly returns to the main ---+ | ||||||
/// } // | | ||||||
/// // | | ||||||
/// fn main() { // | | ||||||
/// let res = a(); // <--------------------------------------+ | ||||||
/// assert_eq!(res, 2); | ||||||
/// } | ||||||
/// ``` | ||||||
/// | ||||||
/// This is an optimization that allows function calls to not exhaust the stack. | ||||||
/// This is most useful for (mutually) recursive algorithms, but may be used in | ||||||
/// other cases too. | ||||||
/// | ||||||
/// It is guaranteed that the call will not cause unbounded stack growth if it | ||||||
/// is part of a recursive cycle in the call graph. | ||||||
/// | ||||||
/// For example note that the functions `halt` and `halt_loop` below are | ||||||
/// identical, they both do nothing, forever. However `stack_overflow` is | ||||||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/// different from them, even though it is written almost identically to | ||||||
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.
Suggested change
|
||||||
/// `halt`, `stack_overflow` exhausts the stack and so causes a stack | ||||||
/// overflow, instead of running forever. | ||||||
/// | ||||||
/// | ||||||
/// ``` | ||||||
/// #![feature(explicit_tail_calls)] | ||||||
/// | ||||||
/// # #[allow(unreachable_code)] | ||||||
/// fn halt() -> ! { | ||||||
/// become halt() | ||||||
/// } | ||||||
/// | ||||||
/// fn halt_loop() -> ! { | ||||||
/// loop {} | ||||||
/// } | ||||||
/// | ||||||
/// # #[allow(unconditional_recursion)] | ||||||
/// fn stack_overflow() -> ! { | ||||||
/// stack_overflow() // implicit return | ||||||
/// } | ||||||
/// ``` | ||||||
/// | ||||||
/// Note that from the algorithmic standpoint loops and tail-calls are | ||||||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/// interchangeable, you can always rewrite a loop to use tail-calls | ||||||
/// instead and vice versa. They are, however, very different in the code | ||||||
/// structure, so sometimes one approach can make more sense that the other. | ||||||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
#[cfg(not(bootstrap))] | ||||||
mod become_keyword {} | ||||||
|
||||||
#[doc(keyword = "self")] | ||||||
// | ||||||
/// The receiver of a method, or the current module. | ||||||
|
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.