Conditions for `drop` to be called during unwinding

⚓ Rust    📅 2025-11-21    👤 surdeus    👁️ 2      

surdeus

Hello,

struct PanicInDrop;
impl Drop for PanicInDrop {
    fn drop(&mut self) {
        panic!()
    }
}
struct PrintInDrop(String);
impl Drop for PrintInDrop {
    fn drop(&mut self) {
        print!("{}.", self.0);
    }
}

fn f(_arg: PanicInDrop) -> PrintInDrop {
    let output = PrintInDrop(String::from("2"));
    {
        PrintInDrop(String::from("3"));
    }
    output
}

fn main() {
    PrintInDrop(String::from("1"));
    f(PanicInDrop);
}

I ran the code above, and this was the content from the standard output:

1.3.

Why was 2. not printed?

Output of cargo --version:

cargo 1.91.1 (ea2d97820 2025-10-10)

6 posts - 3 participants

Read full topic

🏷️ Rust_feed