implement Default for more iterators
Proposal
Problem statement
Sometimes it's useful to be able to create empty iterators without needing to first obtain an empty container to then iterate over, some of the existing iterator types implement Default for this #77 but there are still quite a few missing.
Motivating examples or use cases
I'm currently writing a parser for parsing templates for command lines that are already split into a slice of arguments (written using the Default impls I'd like to add):
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Token {
Char(char),
ArgSeparator,
}
#[derive(Clone, Debug, Default)]
struct Tokens<'a> {
current: std::str::Chars<'a>,
rest: std::slice::Iter<'a, &'a str>,
}
impl<'a> Tokens<'a> {
fn new(args: &'a [&'a str]) -> Self {
Self { rest: args, ..Self::default() }
}
}
impl Iterator for Tokens<'_> {
type Item = Token;
fn next(&mut self) -> Option<Self::Item> {
match self.current.next() {
Some(c) => Some(Token::Char(c)),
None => {
self.current = self.rest.next()?.chars();
Some(Token::ArgSeparator)
}
}
}
}
// code that uses Tokens...
Solution sketch
implement Default for all iterators where the iterator is constructed from some type that has an obvious default value that's empty. So std::str::Chars should be Default since "" is obviously empty. std::array::IntoIter should not be Default since the array type's default wouldn't be empty unless it has zero length. std::env::Args should not be Default since an obvious default value is the program's arguments which isn't usually empty.
The full list of iterator types: https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#implementors
Iterator implementations I found:
iterator implements Default already
For list, see https://github.com/rust-lang/libs-team/issues/656#issuecomment-3306065908
iterator should implement Default
-
core::ascii::EscapeDefault -
core::char::EscapeDebug -
core::char::EscapeDefault -
core::char::EscapeUnicode -
core::char::ToLowercase -
core::char::ToUppercase -
core::char::decode::DecodeUtf16 -
core::error::Source -
core::ffi::c_str::Bytes -
core::iter::adapters::array_chunks::ArrayChunks -
core::iter::adapters::by_ref_sized::ByRefSized -
core::iter::adapters::cycle::Cycle -
core::iter::adapters::filter::Filter -
core::iter::adapters::filter_map::FilterMap -
core::iter::adapters::flatten::FlatMap -
core::iter::adapters::inspect::Inspect -
core::iter::adapters::intersperse::Intersperse -
core::iter::adapters::intersperse::IntersperseWith -
core::iter::adapters::map::Map -
core::iter::adapters::map_while::MapWhile -
core::iter::adapters::map_windows::MapWindows -
core::iter::adapters::peekable::Peekable -
core::iter::adapters::scan::Scan -
core::iter::adapters::skip::Skip -
core::iter::adapters::skip_while::SkipWhile -
core::iter::adapters::step_by::StepBy -
core::iter::adapters::take::Take -
core::iter::adapters::take_while::TakeWhile -
core::iter::adapters::zip::Zip -
core::iter::sources::once::Once -
core::iter::sources::once_with::OnceWith -
core::option::IntoIter -
core::option::Iter -
core::option::IterMut -
core::range::iter::IterRange -
core::range::iter::IterRangeFrom -
core::range::iter::IterRangeInclusive -
core::result::IntoIter -
core::result::Iter -
core::result::IterMut -
core::slice::ascii::EscapeAscii -
core::slice::iter::ArrayWindows -
core::slice::iter::ChunkBy -
core::slice::iter::ChunkByMut -
core::slice::iter::Chunks -
core::slice::iter::ChunksExact -
core::slice::iter::ChunksExactMut -
core::slice::iter::ChunksMut -
core::slice::iter::RChunks -
core::slice::iter::RChunksExact -
core::slice::iter::RChunksExactMut -
core::slice::iter::RChunksMut -
core::slice::iter::RSplit -
core::slice::iter::RSplitMut -
core::slice::iter::RSplitN -
core::slice::iter::RSplitNMut -
core::slice::iter::Split -
core::slice::iter::SplitInclusive -
core::slice::iter::SplitInclusiveMut -
core::slice::iter::SplitMut -
core::slice::iter::SplitN -
core::slice::iter::SplitNMut -
core::slice::iter::Windows -
core::str::iter::Bytes -
core::str::iter::CharIndices -
core::str::iter::Chars -
core::str::iter::EncodeUtf16 -
core::str::iter::EscapeDebug -
core::str::iter::EscapeDefault -
core::str::iter::EscapeUnicode -
core::str::iter::Lines -
core::str::iter::LinesAny -
core::str::iter::MatchIndices -
core::str::iter::Matches -
core::str::iter::RMatchIndices -
core::str::iter::RMatches -
core::str::iter::RSplit -
core::str::iter::RSplitN -
core::str::iter::RSplitTerminator -
core::str::iter::Split -
core::str::iter::SplitAsciiWhitespace -
core::str::iter::SplitInclusive -
core::str::iter::SplitN -
core::str::iter::SplitTerminator -
core::str::iter::SplitWhitespace -
core::str::lossy::Utf8Chunks -
alloc::collections::binary_heap::IntoIterSorted -
alloc::collections::btree::set::Difference -
alloc::collections::btree::set::Intersection -
alloc::collections::btree::set::SymmetricDifference -
alloc::collections::btree::set::Union -
alloc::collections::linked_list::ExtractIf -
alloc::collections::vec_deque::into_iter::IntoIter -
alloc::string::IntoChars -
proc_macro::token_stream::IntoIter -
std::collections::hash::set::Difference -
std::collections::hash::set::Intersection -
std::collections::hash::set::SymmetricDifference -
std::collections::hash::set::Union -
std::os::unix::net::ancillary::Messages -
std::os::windows::ffi::EncodeWide -
std::path::Ancestors -
std::path::Components -
std::path::Iter -
std::process::CommandArgs -
std::process::CommandEnvs
iterator could implement Default but it might make the rest of the iterator slower (e.g. adding a conditional in Drop)
-
alloc::collections::binary_heap::Drain -
alloc::collections::binary_heap::DrainSorted -
alloc::collections::btree::map::ExtractIf -
alloc::collections::btree::set::ExtractIf -
alloc::collections::vec_deque::drain::Drain -
alloc::string::Drain -
alloc::vec::drain::Drain -
alloc::vec::extract_if::ExtractIf -
alloc::vec::splice::Splice -
std::collections::hash::map::Drain -
std::collections::hash::map::ExtractIf -
std::collections::hash::set::Drain -
std::collections::hash::set::ExtractIf -
std::os::unix::net::ancillary::ScmCredentials -
std::os::unix::net::ancillary::ScmRights
iterator shouldn't implement Default
-
core::iter::sources::from_coroutine::FromCoroutine -
core::iter::sources::from_fn::FromFn -
core::iter::sources::repeat::Repeat -
core::iter::sources::repeat_n::RepeatN -
core::iter::sources::repeat_with::RepeatWith -
core::iter::sources::successors::Successors -
std::env::Args -
std::env::ArgsOs -
std::env::SplitPaths -
std::env::Vars -
std::env::VarsOs -
std::fs::ReadDir -
std::io::Bytes -
std::io::Lines -
std::io::Split -
std::net::tcp::Incoming -
std::net::tcp::IntoIncoming -
std::os::unix::net::listener::Incoming
iterator could implement Default but we maybe shouldn't
-
core::ops::range::RangeFrom -
core::ops::range::RangeInclusive -
std::sync::mpmc::IntoIter -
std::sync::mpmc::Iter -
std::sync::mpmc::TryIter -
std::sync::mpsc::IntoIter -
std::sync::mpsc::Iter -
std::sync::mpsc::TryIter
Alternatives
Manually construct iterators from a manually-obtained empty source type and have to implement Default manually.
Links and related work
We did some of this before in #77 but didn't end up handling all the iterator types.
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
seems like my list scraped from https://doc.rust-lang.org/nightly/trait.impl/core/iter/traits/iterator/trait.Iterator.js is still missing some
I'm writing a program to actually find all the impls using rustdoc-types and just noticed std::array::IntoIter<T, N>: Default is stable, which is unexpected given the discussion in #77 that we'd specifically not want that.
Ok, I finished writing that program:
https://github.com/programmerjake/find_iterators_without_default/tree/e77e64d9469d3915a3500699b002688f3fb8e9bb
(edit: I added the full list of types without Default impls to the top comment)
Here's the output for 1.92.0-nightly (4645a7988 2025-09-17):
Types that implement both Iterator and Default
<details>
-
core::array::iter::IntoIter -
core::iter::adapters::chain::Chain -
core::iter::adapters::cloned::Cloned -
core::iter::adapters::copied::Copied -
core::iter::adapters::enumerate::Enumerate -
core::iter::adapters::flatten::Flatten -
core::iter::adapters::fuse::Fuse -
core::iter::adapters::rev::Rev -
core::iter::sources::empty::Empty -
core::ops::range::Range -
core::slice::iter::Iter -
core::slice::iter::IterMut -
alloc::boxed::Box -
alloc::collections::binary_heap::IntoIter -
alloc::collections::binary_heap::Iter -
alloc::collections::btree::map::IntoIter -
alloc::collections::btree::map::IntoKeys -
alloc::collections::btree::map::IntoValues -
alloc::collections::btree::map::Iter -
alloc::collections::btree::map::IterMut -
alloc::collections::btree::map::Keys -
alloc::collections::btree::map::Range -
alloc::collections::btree::map::RangeMut -
alloc::collections::btree::map::Values -
alloc::collections::btree::map::ValuesMut -
alloc::collections::btree::set::IntoIter -
alloc::collections::btree::set::Iter -
alloc::collections::btree::set::Range -
alloc::collections::linked_list::IntoIter -
alloc::collections::linked_list::Iter -
alloc::collections::linked_list::IterMut -
alloc::collections::vec_deque::iter::Iter -
alloc::collections::vec_deque::iter_mut::IterMut -
alloc::vec::into_iter::IntoIter -
std::collections::hash::map::IntoIter -
std::collections::hash::map::IntoKeys -
std::collections::hash::map::IntoValues -
std::collections::hash::map::Iter -
std::collections::hash::map::IterMut -
std::collections::hash::map::Keys -
std::collections::hash::map::Values -
std::collections::hash::map::ValuesMut -
std::collections::hash::set::IntoIter -
std::collections::hash::set::Iter
</details>
Types that implement Iterator but not Default
-
core::ascii::EscapeDefault -
core::char::EscapeDebug -
core::char::EscapeDefault -
core::char::EscapeUnicode -
core::char::ToLowercase -
core::char::ToUppercase -
core::char::decode::DecodeUtf16 -
core::error::Source -
core::ffi::c_str::Bytes -
core::iter::adapters::array_chunks::ArrayChunks -
core::iter::adapters::by_ref_sized::ByRefSized -
core::iter::adapters::cycle::Cycle -
core::iter::adapters::filter::Filter -
core::iter::adapters::filter_map::FilterMap -
core::iter::adapters::flatten::FlatMap -
core::iter::adapters::inspect::Inspect -
core::iter::adapters::intersperse::Intersperse -
core::iter::adapters::intersperse::IntersperseWith -
core::iter::adapters::map::Map -
core::iter::adapters::map_while::MapWhile -
core::iter::adapters::map_windows::MapWindows -
core::iter::adapters::peekable::Peekable -
core::iter::adapters::scan::Scan -
core::iter::adapters::skip::Skip -
core::iter::adapters::skip_while::SkipWhile -
core::iter::adapters::step_by::StepBy -
core::iter::adapters::take::Take -
core::iter::adapters::take_while::TakeWhile -
core::iter::adapters::zip::Zip -
core::iter::sources::from_coroutine::FromCoroutine -
core::iter::sources::from_fn::FromFn -
core::iter::sources::once::Once -
core::iter::sources::once_with::OnceWith -
core::iter::sources::repeat::Repeat -
core::iter::sources::repeat_n::RepeatN -
core::iter::sources::repeat_with::RepeatWith -
core::iter::sources::successors::Successors -
core::ops::range::RangeFrom -
core::ops::range::RangeInclusive -
core::option::IntoIter -
core::option::Iter -
core::option::IterMut -
core::range::iter::IterRange -
core::range::iter::IterRangeFrom -
core::range::iter::IterRangeInclusive -
core::result::IntoIter -
core::result::Iter -
core::result::IterMut -
core::slice::ascii::EscapeAscii -
core::slice::iter::ArrayWindows -
core::slice::iter::ChunkBy -
core::slice::iter::ChunkByMut -
core::slice::iter::Chunks -
core::slice::iter::ChunksExact -
core::slice::iter::ChunksExactMut -
core::slice::iter::ChunksMut -
core::slice::iter::RChunks -
core::slice::iter::RChunksExact -
core::slice::iter::RChunksExactMut -
core::slice::iter::RChunksMut -
core::slice::iter::RSplit -
core::slice::iter::RSplitMut -
core::slice::iter::RSplitN -
core::slice::iter::RSplitNMut -
core::slice::iter::Split -
core::slice::iter::SplitInclusive -
core::slice::iter::SplitInclusiveMut -
core::slice::iter::SplitMut -
core::slice::iter::SplitN -
core::slice::iter::SplitNMut -
core::slice::iter::Windows -
core::str::iter::Bytes -
core::str::iter::CharIndices -
core::str::iter::Chars -
core::str::iter::EncodeUtf16 -
core::str::iter::EscapeDebug -
core::str::iter::EscapeDefault -
core::str::iter::EscapeUnicode -
core::str::iter::Lines -
core::str::iter::LinesAny -
core::str::iter::MatchIndices -
core::str::iter::Matches -
core::str::iter::RMatchIndices -
core::str::iter::RMatches -
core::str::iter::RSplit -
core::str::iter::RSplitN -
core::str::iter::RSplitTerminator -
core::str::iter::Split -
core::str::iter::SplitAsciiWhitespace -
core::str::iter::SplitInclusive -
core::str::iter::SplitN -
core::str::iter::SplitTerminator -
core::str::iter::SplitWhitespace -
core::str::lossy::Utf8Chunks -
alloc::collections::binary_heap::Drain -
alloc::collections::binary_heap::DrainSorted -
alloc::collections::binary_heap::IntoIterSorted -
alloc::collections::btree::map::ExtractIf -
alloc::collections::btree::set::Difference -
alloc::collections::btree::set::ExtractIf -
alloc::collections::btree::set::Intersection -
alloc::collections::btree::set::SymmetricDifference -
alloc::collections::btree::set::Union -
alloc::collections::linked_list::ExtractIf -
alloc::collections::vec_deque::drain::Drain -
alloc::collections::vec_deque::into_iter::IntoIter -
alloc::string::Drain -
alloc::string::IntoChars -
alloc::vec::drain::Drain -
alloc::vec::extract_if::ExtractIf -
alloc::vec::splice::Splice -
std::collections::hash::map::Drain -
std::collections::hash::map::ExtractIf -
std::collections::hash::set::Difference -
std::collections::hash::set::Drain -
std::collections::hash::set::ExtractIf -
std::collections::hash::set::Intersection -
std::collections::hash::set::SymmetricDifference -
std::collections::hash::set::Union -
std::env::Args -
std::env::ArgsOs -
std::env::SplitPaths -
std::env::Vars -
std::env::VarsOs -
std::fs::ReadDir -
std::io::Bytes -
std::io::Lines -
std::io::Split -
std::net::tcp::Incoming -
std::net::tcp::IntoIncoming -
std::os::unix::net::ancillary::Messages -
std::os::unix::net::ancillary::ScmCredentials -
std::os::unix::net::ancillary::ScmRights -
std::os::unix::net::listener::Incoming -
std::os::windows::ffi::EncodeWide -
std::path::Ancestors -
std::path::Components -
std::path::Iter -
std::process::CommandArgs -
std::process::CommandEnvs -
std::sync::mpmc::IntoIter -
std::sync::mpmc::Iter -
std::sync::mpmc::TryIter -
std::sync::mpsc::IntoIter -
std::sync::mpsc::Iter -
std::sync::mpsc::TryIter
The policy as stated in the solution sketch makes sense to me, but a bunch of entries in your list seem dubious, or perhaps require a very loose definition of "constructed from some type that has an obvious default value that's empty":
- While
charimplements Default, that's not an "empty" value for iterator purpose. For example,char::default().escape_debug()is not an empty iterator nor an obvious default value forchar::EscapeDebug. -
result::IntoIter<T>is an empty iterator when constructed fromResult::Err, butResult<T, E>doesn't implementDefaultat all, and if it did implementDefaultreturningErr, that wouldn't be an "empty value" in general (only for the specific purpose of iterating over the result). - Iterator adaptors like
Cyclecan be empty with the right underlying iterator, but since the adapters are parameterized by the underlying iterator, I guess we'd need impls likeimpl<I: Iterator + Default> for Cycle<I>. That doesn't seem in line with the proposed policy because those bounds don't guarantee the default value ofIis empty (cc https://github.com/rust-lang/rust/pull/140985). -
slice::Splitand friends requires picking aFnMuttype for the predicate, and even if you pick one,[].split(|_| todo!())yields an empty slice once, so it's not an empty iterator. Similar problems apply to string splitting iterators. -
error::Sourceas currently defined is never an empty iterator when it's constructed from any error. That aside, while there are some rare error types that implementDefaultand don't yield and (further) sources (e.g.,fmt::Error), but it seems odd to use that to justify an impl forerror::Sourcesin general.
Perhaps the policy needs to be amended to say (1) it must also be obvious which type is the one whose obvious empty default value is used to create the iterator, and (2) the iterator value constructed this way must be empty.
yeah, after some point I kinda forgot the policy I wrote down and went more by which iterators I think being empty is a reasonable default (going off the core::array::IntoIter precedent I guess), regardless of how you can construct them.
for Cycle<T>, I think it's reasonable to assume T: Default generally means the inner iterator's default is empty, but even if it isn't empty, the default should just be T::default().cycle()
I'm writing a program to actually find all the impls using
rustdoc-typesand just noticedstd::array::IntoIter<T, N>: Defaultis stable, which is unexpected given the discussion in #77 that we'd specifically not want that.
Some discussion in the PR.
I'm planning on adjusting this ACP to propose that all iterators by convention should default to an empty iterator, unless the type comes with a strong expectation of the default being non-empty (e.g. env::Args), in which case they shouldn't implement Default.
I may take a while to get around to doing that, sorry in advance.
One question:
ExtractIf has an F type param that represents the filter, which may not have a Default. What does a proper Default for it look like?
I was assuming it'd just implement Default where F: Default -- that also seems like a good motivation for having more function items and capture-less lambdas implement Default
We had a preliminary discussion about this in today's @rust-lang/libs-api meeting.
We observed some additional cases where we don't want to implement Default. For example, we don't want to implement Default for Matches or MatchIndices, because that would require synthesizing the generic Pattern type. Likewise for Map or Split which would require synthesizing a function.
Please drop any cases that would require synthesizing a user-controlled Pattern or function or other generic type. That'll make the remaining list easier to review.
(Cases that might require a std-specified type internally are okay; for instance, Lines should be fine.)
Can you also separate out the cases that are wrappers around another iterator (which will require I: Default) from the cases that aren't?
Once that's done we'll take another look at the remaining items. Thank you!