std::mem::drop - Rust
Description: API documentation for the Rust `drop` fn in crate `std`.
Keywords: rust, rustlang, rust-lang, drop

# 1.0.0[−][src]Function std::mem::drop

    pub fn drop<T>(_x: T)

Disposes of a value.

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`, e.g. integers. Such values are copied and then moved into the function, so the value persists after this function call.

This function is not magic; it is literally defined as

    pub fn drop<T>(_x: T) { }Run

Because `_x` is moved into the function, it is automatically dropped before the function returns.

# Examples

Basic usage:

    let v = vec![1, 2, 3];

    drop(v); // explicitly drop the vectorRun

Borrows are based on lexical scope, so this produces an error:

ⓘThis example deliberately fails to compile

    let mut v = vec![1, 2, 3];
    let x = &v[0];

    drop(x); // explicitly drop the reference, but the borrow still exists

    v.push(4); // error: cannot borrow `v` as mutable because it is also
               // borrowed as immutableRun

An inner scope is needed to fix this:

    let mut v = vec![1, 2, 3];

    {
        let x = &v[0];

        drop(x); // this is now redundant, as `x` is going out of scope anyway
    }

    v.push(4); // no problemsRun

Since `RefCell` enforces the borrow rules at runtime, `drop` can release a `RefCell` borrow:

    use std::cell::RefCell;

    let x = RefCell::new(1);

    let mut mutable_borrow = x.borrow_mut();
    *mutable_borrow = 1;

    drop(mutable_borrow); // relinquish the mutable borrow on this slot

    let borrow = x.borrow();
    println!("{}", *borrow);Run

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 availableRun

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)