Repository navigation
Provide a means of turning iterators into fixed-size arrays #81615
Description
Now that min_const_generics is approaching there is a great deal of new support for working with fixed-sized arrays, such as std::array::IntoIter. But while converting from an array to an iterator is now well-supported, the reverse is lacking:
let mut a = std::array::IntoIter::new([1,2,3]);
let b: [i32; 3] = a.collect(); // the trait `FromIterator<{integer}>` is not implemented for `[i32; 3]`It is roundaboutly possible to do the conversion by going through Vec:
let mut a = std::array::IntoIter::new([1,2,3]);
let b: [i32; 3] = a.collect::<Vec<_>>().try_into().unwrap();But that isn't no_std compatible, and even with std there should be no need to allocate here.
The non-allocating way of converting the array presupposes a fair bit of familiarity with the stdlib, unsafe code, and unstable features:
#![feature(maybe_uninit_uninit_array)]
#![feature(maybe_uninit_array_assume_init)]
let mut array: [MaybeUninit<T>; N] = MaybeUninit::uninit_array();
for i in 0..N {
array[i] = MaybeUninit::new(iter.next().unwrap());
}
let array = unsafe {
MaybeUninit::array_assume_init(array)
};...which suggests this is a prime candidate for stdlib inclusion.
The first problem is what the signature should be. There's no way to statically guarantee that an iterator is any given length, so (assuming that you don't want to panic) what is the return type when the iterator might be too short?
-> [T; N]: straightforward, but you'd have to haveT: Default, which limits its usefulness. Furthermore this uses in-band signaling to mask what is probably an error (passing in a too-small iterator), which feels user-hostile. This seems little better than panicking.-> [Option<T>; N]: the obvious solution to indicate potentially-missing data, but likely annoying to work with in the success case. And sadly the existing blanket impl that ordinarily allows you.collectfrom from a collection of options into an option of a collection can't be leveraged here because you still don't have an impl ofFromIterator<T> for [T; N]. So if you actually want a[T; N]you're left with manually iterating over what was returned, which is to say, you're no better off than having the iterator you started out with!-> Option<[T; N]>: the simplest solution that doesn't totally ignore errors. This would be consistent withstd::slice::array_windowsfor producing None when a function cannot construct a fixed-size array due to a too-small iterator. However, it unfortunately seems less quite a bit less recoverable in the failure case than the previous approach.-> Result<[T; N], U>: same as the previous, though it's possible you could pass something useful back in the error slot. However, as long as the iterator is by-value and unless it's limited to ExactSizeIterators, it might be tricky to pass the data back.-> ArrayVec<T, N>: this would be a new type designed to have infallible conversion fromFromIterator. Actually extracting the fixed-size array would be done through APIs on this type, which avoids some problems in the next section.
IMO approaches 1 and 2 are non-starters.
The second problem is how to perform the conversion.
FromIterator: the obvious approach, however, it cannot be used with return type 3 from the prior section. This is because of the aforementioned blanket impl for collecting a collection-of-options into an option-of-collections, which conflicts with any attempt to implFromIterator<T> for Option<[T; N]>. I think even specialization can't solve this?TryFrom: theoretically you could forgo an impl ofFromIterator<T>and instead implTryFrom<I: IntoIterator<Item=T>>; hacky, but at least you're still using some standard conversion type. Sadly, Invalid collision with TryFrom implementation? #50133 makes it impossible to actually write this impl; people claim that specialization could theoretically address that, but I don't know enough about the current implementation to know if it's sufficient for this case.- Introduce a new
TryFromIteratortrait. This would work, but this also implies a newTryIntoIteratortrait andIterator::try_collect. Likely the most principled approach. - A new
std::array::TryCollecttrait, impl'd forI: IntoIterator. Less principled than the prior approach but less machinery for if you happen to thinkTryFromIteratorandTryIntoIteratorwouldn't be broadly useful. - A new
std::array::from_iterfunction. The simplest and least general approach. Less consistent with.collectthan the previous approach, but broadly consistent withstd::array::IntoIter(although that itself is considered a temporary stopgap until.into_iterworks on arrays). Similarly, could be an inherent associated function impl'd on[T; N]. - Come up with some wacky const-generics shenanigans that would allow
FromIterator<T> for [T; N]to Just Work, possibly via introducing a new const-generics aware version of ExactSizeIterator. If this could be done it would unquestionably be the most promising way to proceed, but I don't have the faintest idea where to begin. - A new method
Iterator::collect_arrayas a hardcoded alternative to collect. Similarly, an Iterator-specific equivalent ofslice::array_chunkscould fill the same role while also being potentially more flexible.
Any other suggestions?
Activity
the8472 commentedon Feb 1, 2021
Existing PRs #69985, #75644 and #79659. But It's great to have a place to consolidate discussion since there are so many ways to skin this cat.
Additional options.
T: Defaultand fill the array when the iterator falls short. Or let the user provide a lambda to fill them.ArrayVecbstrie commentedon Feb 1, 2021
This is an interesting alternative, and has precedence with
Option::unwrap_or_default(which takes a value, not a lambda). However, it would preclude using any of the existing conversion traits.This alternative is mentioned above. The difficulty of handling the error case means that I wouldn't want this to be the only way to convert an iterator into an array, but I wouldn't be opposed to it existing alongside of another approach.
Thinking about it, I suppose this approach could technically be considered equivalent to the previously-mentioned "let the user provide a [value]" approach, assuming that the user does something like
x.into_iter().chain(std::iter::repeat(FOO)).collect(); this would guarantee any would-be "empty" slots in the array would instead be filled in withFOO. A bit clunky, though.Can you elaborate on what this is?
the8472 commentedon Feb 1, 2021
Something like
This has been proposed in other conversations as it would help in several places in the standard library where an
[T; N]is gradually initialized. And it would also help in this case since we could just return that instead of[T; N]directly. The user could then extract the array whenlen == Nor handle the shorter than expected array in some other way.It's similar to returning a tuple of a partially initialized array and a usize that tells you how many members have been initialized, except that it's safe to use.
With this we could do
iter.collect::<ArrayVec<_, 16>>()the8472 commentedon Feb 1, 2021
#81382 (comment) suggests
Iterator::array_windows()if that can be implemented (the storage requirements for the array were in question) then a similarIterator::array_chunks()should be possible. With that one could calliter.array_chunks().next()That is actually more general than using
FromIteratororTryFrombecause it does not consume the iterator and thus allows multiple arrays to be extracted if it has enough elements.#79659 (comment) also suggests something similar, albeit as an iterator method instead of an iterator adapter.
I'm not sure if that's possible. Iterators are stateful, they can be in a partially exhausted without changing their type. So the remaining number of elements can't be determined at compile time.
bstrie commentedon Feb 1, 2021
Hm, I'm very intrigued by the suggestion of
Iterator::array_chunksas a solution to this. It's usable in the simple case, generalizes to advanced cases, has symmetry with existing APIs, and could sidestep the return type discussion by making it natural to return a None when the iterator no longer has sufficient elements. However, would it actually be possible for this to support aremaindermethod likearray_chunkshas in order to handle the case where the iterator length isn't a multiple of the array length? What would the return type be? The hypotheticalIterator::array_windowsavoids the problem of having a remainder, but for that you'd needT: Copy. I've added this to the list of suggestions.I've also added
ArrayVecto the list of potential return types.amosonn commentedon Feb 1, 2021
What about
Iterator::next_n<N: const usize>? This allows to pull an array, and then keep pulling normally from the iterator.Iterator::array_chunkscan be trivially implemented in terms of this.As for the return type, it is worth noting: Whereas in an array, if some elements are "gone" (e.g. when a None is returned from
array_windows), in an iterator this is more of a problem. Maybe something likeResult<[T; N], (usize, [MaybeUninit<T>; N])>? (Or, of course,Result<[T; N], ArrayVec<T, N>>, though then it might be easier to just return theArrayVec).the8472 commentedon Feb 1, 2021
@bstrie
Probably
std::iter::ArrayChunksif it is an iterator adapter.Well, that's just the return type question again, but this time for the associated type, i.e.
type Item = ???.If
Item = [T; N]thennext()then there are two ways to implement this. Eithernext()just iterates until it can fill the array andreturns that and otherwise throws the partial result away... or it keeps internal state which can be retrieved through an additional method from the adapter. E.g. the number of elements it discarded. Or the elements it discarded inside a
Vec<_>, which will never be allocated if your iterator is an exact multiple of N. Or an internal[MaybeUninit<T>; N]buffer to keep the remainder.Or it could have any of the other discussed result types, e.g.
type Item = ArrayVec@amosonn
They're actually a bit different, both having advantages and disadvantages.
If
next_n()is implemented all the way down to the iterator source then it can be executed without discarding any elements. But that behavior would have to be optional, the default implementation would have to discard elements when there's not enough left to fill the array, at least for things that aren'tTrustedLen/ExactSizeIterator/TrustedRandomAccess.array_chunks()on the other hand could keep state in its adapter and thus make things recoverable. But it's potentially less efficient since it can't just pass entire arrays through the iterator pipeline for each step, and it would probably be larger.So those should be considered as distinct options.
bstrie commentedon Feb 1, 2021
I was mistaken about something in the original post; while it is impossible to impl
FromIterator<T> for Result<[T; N], Foo>due to the conflicting impl, it's not impossible to implFromIterator<T> for Result<[T; N], Foo<T>>. I can't say I understand why the latter doesn't conflict, but that does much lessen any need forTryFromIteratorfrom a technical perspective.amosonn commentedon Feb 2, 2021
@the8472
That's a good point! Getting the rest from the adapter definitely avoids the problem of the complicated signature. The downside is that you can forget to look at it, as opposed to a Result, and also that this pattern does not play well with a for loop.
usbalbin commentedon Feb 2, 2021
There were some ideas in #80094, not sure how/if that would fit in with the current Iterators though...
@leonardo-m 's comment:
bstrie commentedon Feb 2, 2021
I'm not entirely clear what that's suggesting, but it looks like they're asking for an iterator where the length is encoded in the type somehow, which means that the type of the iterator would need to change with every call to
next. Rust types don't have the ability to mutate their type, so you'd need a totally separate interface where each call tonextreturns both a value and a new, smaller iterator. I don't think that helps with this issue.the8472 commentedon Feb 2, 2021
A somewhat tangentional option would be to have a separate iterator source method on the collections. This has the benefit of guaranteed access to the size, so the iterator will know in advance how large the array can be, and the remainder of the elements will remain in the collection.
Similiar to
vec.drain()which does not consume the vec itself but does consume elements within the vec. I.e. the iterated elements are not borrows.Vec::arrays::<N>(&mut self) -> impl Iterator<Item=[T; N]>. You could directly pop off an array vianext(). Or you could process it further via a chainvec.arrays::<5>().map(|ary| ary.map(|e| e + 1)).next(). The latter map is the inherent array method, not an iterator one.This doesn't solve the problem of turning arbitrary iterators into an array, but it directly lets you get arrays out of collections and process them. And it avoids the impedance mismatch of iterator methods such as
filterwhich produce an unknown amount of elements.6 remaining items
bstrie commentedon Feb 28, 2021
Hm, that's an intriguing possibility. On the other hand it doesn't actually solve the issue for users who didn't start out with a fixed-size array in the first place. Which is to say, even if we had such a size-aware array-only iterator, we'd probably still want one of the other solutions in this thread to fill in the gaps. I'd say your proposal is orthogonal to this issue and is worth being considered separately.
Agreed that having real-world use cases would be a big help here.
the8472 commentedon Mar 15, 2021
There already is a backlink, but just to make it explicit: There's a RFC PR for ArrayVec. If that gets blessed this would at least settle a good chunk of the return type questions.
Aplet123 commentedon Feb 14, 2022
I made a potential implementation of approach 4 that passes data back with any type that implements
FromIterator<A>. Note that people might not actually want any data back on failure, so there might have to be some accompanying zero-sized type that implementsFromIteratorto avoid allocation.Of course, this is probably completely unnecessary once
ArrayVecbecomes a thing.Auto merge of #84111 - bstrie:hashfrom, r=joshtriplett
Auto merge of rust-lang#84111 - bstrie:hashfrom, r=joshtriplett
tyilo commentedon Aug 1, 2025
I would prefer an API where the signature is something like
fn(impl Iterator<Item = T>) -> Result<[T; N], ...>and where an error is returned for both iterators with too few and too many elements.The error type could be something like:
tisonkun commentedon Oct 27, 2025
This can be closed as now we have
next_chunkto cover the usage: #98326tyilo commentedon Oct 27, 2025
No that doesn't error when there are too many items left.
leonardo-m commentedon Oct 27, 2025
I think next_chunk isn't generating efficient asm when the output array produced is largish (like 1000-20000 ints). So I use my own libs for this problem for now.
isuffix commentedon Nov 25, 2025
This is making progress with #149266 🎉