Function mycelium_util::mem::drop
1.0.0 · source · pub fn drop<T>(_x: T)
Expand description
Disposes of a value.
This does so by calling 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
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