-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Document drop more. #42159
Document drop more. #42159
Changes from 4 commits
14b767d
ca909c8
d7927ff
5f4b0ff
b41b294
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,6 +153,14 @@ use marker::Unsize; | |
/// The `Drop` trait is used to run some code when a value goes out of scope. | ||
/// This is sometimes called a 'destructor'. | ||
/// | ||
/// When a value goes out of scope, if it implements this trait, it will have | ||
/// its `drop` method called. Then any fields the value contains will also | ||
/// be dropped recursively. | ||
/// | ||
/// Because of the recursive dropping, even for types that do not implement | ||
/// this trait, you do not need to implement this trait unless your type | ||
/// needs its own destructor logic. | ||
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. "even for types that do not implement this trait, you do not need to implement this trait" doesn't parse to me :) 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. This is what happens when you stare at the screen for half an hour wondering what to write. |
||
/// | ||
/// # Examples | ||
/// | ||
/// A trivial implementation of `Drop`. The `drop` method is called when `_x` | ||
|
@@ -171,6 +179,44 @@ use marker::Unsize; | |
/// let _x = HasDrop; | ||
/// } | ||
/// ``` | ||
/// | ||
/// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the | ||
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. First sentence is incomplete? 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. Only as much as the first sentence to the previous example which says "A trivial implementation of 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. Yeah; I think this is fine, though personally, I tend to use fragments like this for the short ones, and since there's more here, I'd probably make it a full sentence. NBD |
||
/// `drop` method will be called for `Outer` and then the `drop` method for | ||
/// `Inner` will be called. Therefore `main` prints `Dropping Outer!` and then | ||
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. repetitive wording "the 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. Can do. |
||
/// `Dropping Inner!`. | ||
/// | ||
/// ``` | ||
/// struct Inner; | ||
/// struct Outer(Inner); | ||
/// | ||
/// impl Drop for Inner { | ||
/// fn drop(&mut self) { | ||
/// println!("Dropping Inner!"); | ||
/// } | ||
/// } | ||
/// | ||
/// impl Drop for Outer { | ||
/// fn drop(&mut self) { | ||
/// println!("Dropping Outer!"); | ||
/// } | ||
/// } | ||
/// | ||
/// fn main() { | ||
/// let _x = Outer(Inner); | ||
/// } | ||
/// ``` | ||
/// | ||
/// Because variables are dropped in the reverse order they are declared, | ||
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. Is "declared" the right term here? 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. yes |
||
/// `main` will print `Declared second!` and then `Declared first!`. | ||
/// | ||
/// ``` | ||
/// struct PrintOnDrop(&'static str); | ||
/// | ||
/// fn main() { | ||
/// let _first = PrintOnDrop("Declared first!"); | ||
/// let _second = PrintOnDrop("Declared second!"); | ||
/// } | ||
/// ``` | ||
#[lang = "drop"] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait Drop { | ||
|
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.
This is a bit repetitive w.r.t "when a value goes out of scope".
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.
That's fine, as the first line is a summary, and this is the full explanation.