Panic in Vec drop

โš“ Rust    ๐Ÿ“… 2026-02-27    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 3      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Panic in Vec drop

What happens after one of the elements of a Vec panics when it drops?

I donโ€™t think it is defined anywhere, but my mental model is that the execution of Vec::drop function would be interrupted, so any elements that have not already been dropped will not be dropped.

I think if you write a Vec in Rust without doing something strange ( such as catching the panic ), this is what would happen.

However it seems that std::Vec DOES โ€œcatch the panicโ€ (somehow - I donโ€™t know how) and does drop more elements.
Playground example

Perhaps this is all a little academic, as also in my mental model is drop functions should/must not panic, as it sabotages panic recovery, but I just want to understand the situation.

18 posts - 5 participants

Read full topic

๐Ÿท๏ธ Rust_feed