When drop is called for a mut String variable?

fn foo() { 
    let mut s = String::from("a string on the heap");
    s = String::("another string on the heap"); // new allocation 
} // when code reaches here drop is called

When code reaches } drop is called, deallocation occurs for the string "another string on the heap". How is the first string deallocated?

At the time where s is assigned. The old value is dropped before the new one takes its place.

1 Like

Thanks a lot! This is reasonable.