Skip to content

Commit 012f369

Browse files
committed
Add note about Copy for drop()
1 parent 5ca60d9 commit 012f369

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcore/mem.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
434434
/// While this does call the argument's implementation of `Drop`, it will not
435435
/// release any borrows, as borrows are based on lexical scope.
436436
///
437+
/// This effectively does nothing for
438+
/// [types which implement `Copy`](../../book/ownership.html#copy-types),
439+
/// e.g. integers. Such values are copied and _then_ moved into the function,
440+
/// so the value persists after this function call.
441+
///
437442
/// # Examples
438443
///
439444
/// Basic usage:
@@ -486,6 +491,21 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
486491
/// let borrow = x.borrow();
487492
/// println!("{}", *borrow);
488493
/// ```
494+
///
495+
/// Integers and other types implementing `Copy` are unaffected by `drop()`
496+
///
497+
/// ```
498+
/// #[derive(Copy, Clone)]
499+
/// struct Foo(u8);
500+
///
501+
/// let x = 1;
502+
/// let y = Foo(2);
503+
/// drop(x); // a copy of `x` is moved and dropped
504+
/// drop(y); // a copy of `y` is moved and dropped
505+
///
506+
/// println!("x: {}, y: {}", x, y.0); // still available
507+
/// ```
508+
///
489509
#[inline]
490510
#[stable(feature = "rust1", since = "1.0.0")]
491511
pub fn drop<T>(_x: T) { }

0 commit comments

Comments
 (0)