1. #[derive(Debug)]
    2. struct Appellation {
    3. name: String
    4. }
    5. impl Drop for Appellation {
    6. fn drop(&mut self) {
    7. println!("{:?} dropped", self);
    8. }
    9. }
    10. fn complicated_condition() -> bool {
    11. true
    12. }
    13. fn main() {
    14. let p;
    15. {
    16. let mut q = Appellation {
    17. name: "hello".to_string(),
    18. };
    19. if complicated_condition() {
    20. p = q;
    21. }
    22. }
    23. println!("who drop first?");
    24. }

    上面那个代码片段中,Appellation何时drop取决于complicated_condition的返回值,如果complicated_condition返回true,那么q的所有权即转移到了p上,当q goes out of scope时,p还活着,所以Appellation将在 println后 drop,如果complicated_condition返回false,则当q goes out of scope时,Appellation就会drop,将在println之前