Pre-RFC: Lazy Drain and Self-Exhausting Iterators

That's not quite right. The leak amplification happens there, but the actual exhaustion is definitely in Drop: https://doc.rust-lang.org/src/alloc/vec.rs.html#2488

This ought to be a pretty easy method to add. As a sketch, I think it'd be something like this:

impl<'a, T> Drain<'a, T> {
    fn keep_remainder(self) { 
        // Move the yet-to-be-iterated range into the "tail to keep"
        self.tail_start -= self.iter.len();
        self.tail_len += self.iter.len();
        // Make sure dropping ourself doesn't re-iterate those
        self.iter = [].iter();
    }
}
1 Like