pub fn drop<T>(_x: T)
Disposes of a value.
This does call the argument's implementation of Drop
.
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
Because _x
is moved into the function, it is automatically dropped before the function returns.
Basic usage:
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
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/mem/fn.drop.html