std-function

Rust drop(): end ownership early

The standard drop function consumes its argument. For a non-Copy owner, this can trigger destruction before the normal end of its scope. For Copy values, the original remains usable. Prefer a small scope when it expresses the intended lifetime more clearly. Expected output (not executed): Released Next task Why: The marker is moved into drop and destroyed before the following print. Common mistake: Calling drop on a shared reference when the goal is to destroy its referent.

Formal Syntax & Structure

rust syntaxrust
pub fn drop<T>(_x: T)

Working Example

Example Snippetrust
struct Marker;impl Drop for Marker {    fn drop(&mut self) { println!("Released"); }}fn main() {    let marker = Marker;    drop(marker);    println!("Next task");}
Rust Reference