First and foremost, I apologize if this is not the right channel for this discussion/suggestion of feature or if something similar has already been discussed previously.
In Rust we have collect. An issue of it is, that for example, if we are OOM we might fail and thus get an abort (or panic if you configure it to do so). There are some scenarios where this is not ideal, for example scenarios where you are able to free some memory and then retry the operation for example (let say that you have a limited pool of memory for layout X and multiples thereof which you control for example on an embedded system), or when you absolutely need to keep a service running still until a certain moment (for example, you don't accept new connections, keep the current ones alive and ends the service only when all connections have been dropped).
For this, it would be interesting, when working with iterators, to have a try_collect method (and as such, a Trait or whatever on how this is implemented for the collect method). It should then return a Result<CollectionType, Error>. One of the nice things is that something as such would allow for seamless collection keeping a similar interface for fixed sized arrays, as for example:
collection.iter().filter(|item|
/*yada yada code if some condition met, Some(item) else None */
).try_collect<[T : N]>()
}
If N and the amount of items that have been filtered do not match, then collection would fail. I am aware that are multiple ways of avoiding this scenario that avoid non fallible operations, such as, for example, creating a Vector first and trying to preallocate the space needed or having a fixed sized array of MaybeUninit, but I would argue that having a similar interface would allow for more ergonomic and readable code, keeping code, for the lack of a better word, somewhat 'standardized'.
Result<Collection<T>, Error> does implement FromIterator, allowing you to collect an iterator of Result<T> into Collection<T> using Iterator::collect. The operation fails early on the first Err(Error) encountered. This does not work for arrays though, as they are not supported as a collection type, i.e. they don't themselves implement FromIterator.
itertools has a collect_array method, but it doesn't offer the same ergonomics of collecting Result<T, Error> into Result<[T; N], Error>, only into Option<[Result<T, Error>; N]>.
Albeit useful, it is not the case of what I have tried to explain. Maybe English not being my first language doesn't help. This handles the case where the operation inside fails, not the case where creating the collection itself fails.
It does something different from what you're asking for, though, I think. And I'd suggest not trying to include the array case and the collection case in the same thing.
Thank you for mentioning the existence of the try_collect, albeit yes, it does a different thing than what I was asking for. What I was looking for is specifically for something that handles the case when creating (provisioning for memory mainly) the collection fails.
I don't think there's any good and automatic way to achieve fallible and non-panicking/aborting collection across all collection types, so you'd probably be looking at a dedicated trait.
Or a new dedicated FromIterator function that only has the qualities you want when a collection overrides it, I suppose... but the interaction with impl FromIterator<Result<A, E>> for Result<V, E> could be awkward, and having to check for overrides and documented guarantees would be less than ideal too.