What happens when a variable goes out of scope

Rust automatically frees memory via `drop` when a variable goes out of scope unless it was moved or copied.

When a variable goes out of scope in Rust, the compiler automatically calls drop to free its memory if it owns data on the heap. If the variable was moved or implements the Copy trait, no special cleanup occurs.

fn main() {
    let s = String::from("hello"); // s comes into scope
    // s goes out of scope here and `drop` is called
}

fn takes_ownership(some_string: String) {
    println!("{some_string}");
} // some_string goes out of scope and `drop` is called