Does Rust drop objects immediately?

Does rust drops object immediately after it last used or drop it when exits the current scope?

fn test() {
    let x = "1".to_owned();

    // does x drops here? 

    let y = "1".to_owned();

   // or x drops here?
}

Variables are always* only dropped at the end of their scope, in reverse order of declaration.

fn test() {
    let x = "1".to_owned();
    let y = "1".to_owned();

   // y drops here
   // x drops here
}

* of course variables that have been moved out of don’t actually drop anything. This is why manually calling β€˜drop(x)’ can be used to drop x eariler.

5 Likes