Content-Length: 11031 | pFad | https://doc.rust-lang.org/reference/../std/ptr/../mem/fn.drop.html

drop in std::mem - Rust

drop

Function drop 

1.0.0 (const: unstable) · Source
pub fn drop<T>(_x: T)
Expand description

Disposes of a value.

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) {}

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 vector

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);

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








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://doc.rust-lang.org/reference/../std/ptr/../mem/fn.drop.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy