ownership

Rust Assignment destroys the old owned value

Assigning a replacement to an initialized mutable place normally destroys the previous value as part of the assignment. A move into an uninitialized place has no old value to destroy. Expected output (not executed): Start draft End draft Start final End final Why: The first job is destroyed on replacement; the second at the end of main. Common mistake: Assuming reassignment retains the previous resource until the end of the scope.

Formal Syntax & Structure

rust syntaxrust
owner = replacement;

Working Example

Example Snippetrust
struct Job(&'static str);impl Drop for Job {    fn drop(&mut self) { println!("End {}", self.0); }}fn main() {    let mut job = Job("draft");    println!("Start {}", job.0);    job = Job("final");    println!("Start {}", job.0);}
Rust Reference