Add Default impls for iterators and adapters (original) (raw)

Proposal

Problem statement

Currently many iterators don't implement Default which means one can't derive(Default) structs containing them, mem::take() or Cell::take them.

Motivation, use-cases

https://github.com/rust-lang/rust/blob/dc2d232c7485c60dd856f8b9aee83426492d4661/library/alloc/src/vec/drain.rs#L129

    let iter = mem::replace(&mut self.iter, (&mut []).iter());

could be replaced with

    let iter = mem::take(&mut self.iter);

Solution sketches

Iterator sources should implement Default when it is unambiguous that the default should be an empty iterator. Examples:

Adapters should implement it when the inner iterator does and when they don't have any closure that would have to be materialized

Examples where it should not be implemented

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.