我正在查看 DrainFilter 的 drop 实现,以确保它删除所有剩余元素,并且我注意到它在每个drop(item);. 这个守卫有什么用?除非这个drop(item);电话引起恐慌,否则我看不出它的目的。难道只是为了确保即使发生恐慌也能删除其余的项目吗?为什么不使用catch_unwind?
相关drop impl:
impl<T, F> Drop for DrainFilter<'_, T, F>
where
F: FnMut(&mut T) -> bool,
{
fn drop(&mut self) {
struct DropGuard<'r, 'a, T, F>(&'r mut DrainFilter<'a, T, F>)
where
F: FnMut(&mut T) -> bool;
impl<'r, 'a, T, F> Drop for DropGuard<'r, 'a, T, F>
where
F: FnMut(&mut T) -> bool,
{
fn drop(&mut self) {
self.0.for_each(drop);
}
}
while let Some(item) = self.next() {
let guard = DropGuard(self);
drop(item); …Run Code Online (Sandbox Code Playgroud) rust ×1