150150mod arith;
151151mod bit;
152152mod deref;
153+ mod drop;
153154mod function;
154155mod index;
155156mod place;
@@ -170,6 +171,9 @@ pub use self::bit::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssig
170171#[ stable( feature = "rust1" , since = "1.0.0" ) ]
171172pub use self :: deref:: { Deref , DerefMut } ;
172173
174+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
175+ pub use self :: drop:: Drop ;
176+
173177#[ stable( feature = "rust1" , since = "1.0.0" ) ]
174178pub use self :: function:: { Fn , FnMut , FnOnce } ;
175179
@@ -193,93 +197,3 @@ pub use self::place::{Place, Placer, InPlace, Boxed, BoxPlace};
193197
194198#[ unstable( feature = "coerce_unsized" , issue = "27732" ) ]
195199pub use self :: unsize:: CoerceUnsized ;
196-
197- /// The `Drop` trait is used to run some code when a value goes out of scope.
198- /// This is sometimes called a 'destructor'.
199- ///
200- /// When a value goes out of scope, if it implements this trait, it will have
201- /// its `drop` method called. Then any fields the value contains will also
202- /// be dropped recursively.
203- ///
204- /// Because of the recursive dropping, you do not need to implement this trait
205- /// unless your type needs its own destructor logic.
206- ///
207- /// # Examples
208- ///
209- /// A trivial implementation of `Drop`. The `drop` method is called when `_x`
210- /// goes out of scope, and therefore `main` prints `Dropping!`.
211- ///
212- /// ```
213- /// struct HasDrop;
214- ///
215- /// impl Drop for HasDrop {
216- /// fn drop(&mut self) {
217- /// println!("Dropping!");
218- /// }
219- /// }
220- ///
221- /// fn main() {
222- /// let _x = HasDrop;
223- /// }
224- /// ```
225- ///
226- /// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
227- /// `drop` method will be called first for `Outer`, then for `Inner`. Therefore
228- /// `main` prints `Dropping Outer!` and then `Dropping Inner!`.
229- ///
230- /// ```
231- /// struct Inner;
232- /// struct Outer(Inner);
233- ///
234- /// impl Drop for Inner {
235- /// fn drop(&mut self) {
236- /// println!("Dropping Inner!");
237- /// }
238- /// }
239- ///
240- /// impl Drop for Outer {
241- /// fn drop(&mut self) {
242- /// println!("Dropping Outer!");
243- /// }
244- /// }
245- ///
246- /// fn main() {
247- /// let _x = Outer(Inner);
248- /// }
249- /// ```
250- ///
251- /// Because variables are dropped in the reverse order they are declared,
252- /// `main` will print `Declared second!` and then `Declared first!`.
253- ///
254- /// ```
255- /// struct PrintOnDrop(&'static str);
256- ///
257- /// fn main() {
258- /// let _first = PrintOnDrop("Declared first!");
259- /// let _second = PrintOnDrop("Declared second!");
260- /// }
261- /// ```
262- #[ lang = "drop" ]
263- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
264- pub trait Drop {
265- /// A method called when the value goes out of scope.
266- ///
267- /// When this method has been called, `self` has not yet been deallocated.
268- /// If it were, `self` would be a dangling reference.
269- ///
270- /// After this function is over, the memory of `self` will be deallocated.
271- ///
272- /// This function cannot be called explicitly. This is compiler error
273- /// [E0040]. However, the [`std::mem::drop`] function in the prelude can be
274- /// used to call the argument's `Drop` implementation.
275- ///
276- /// [E0040]: ../../error-index.html#E0040
277- /// [`std::mem::drop`]: ../../std/mem/fn.drop.html
278- ///
279- /// # Panics
280- ///
281- /// Given that a `panic!` will call `drop()` as it unwinds, any `panic!` in
282- /// a `drop()` implementation will likely abort.
283- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
284- fn drop ( & mut self ) ;
285- }
0 commit comments