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.

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.