From 012f36947afb3362a686f3d85f6398c8886b9255 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 21 Sep 2015 08:10:30 +0530 Subject: [PATCH] Add note about Copy for drop() --- src/libcore/mem.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 48d003c2cffba..193b8d6d620de 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -434,6 +434,11 @@ pub fn replace(dest: &mut T, mut src: T) -> T { /// While this does call the argument's implementation of `Drop`, it will not /// release any borrows, as borrows are based on lexical scope. /// +/// This effectively does nothing for +/// [types which implement `Copy`](../../book/ownership.html#copy-types), +/// e.g. integers. Such values are copied and _then_ moved into the function, +/// so the value persists after this function call. +/// /// # Examples /// /// Basic usage: @@ -486,6 +491,21 @@ pub fn replace(dest: &mut T, mut src: T) -> T { /// let borrow = x.borrow(); /// println!("{}", *borrow); /// ``` +/// +/// Integers and other types implementing `Copy` are unaffected by `drop()` +/// +/// ``` +/// #[derive(Copy, Clone)] +/// struct Foo(u8); +/// +/// let x = 1; +/// let y = Foo(2); +/// drop(x); // a copy of `x` is moved and dropped +/// drop(y); // a copy of `y` is moved and dropped +/// +/// println!("x: {}, y: {}", x, y.0); // still available +/// ``` +/// #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn drop(_x: T) { }