Repository: rust-itertools/itertools (language: rust)

Issue:
Feature request: try_collect
I often find myself wanting to write

```rust
some_iter
    .map(Type::fallible_conversion)
    .collect()?
```

This requires a turbofish for `collect` though, because both `?` and `collect` are generic. In every case I've had, I wanted the `FromIterator` implementation for `Result`. A dedicated `try_collect` method that's only available for `Result` iterators would solve this.


Discussion / hints:
This should work:

```rust
fn try_collect<T, U, E>(self) -> Result<U, E>
where
    Self: Sized + Iterator<Item = Result<T, E>>,
    Result<U, E>: FromIterator<Result<T, E>>,
{
    self.collect()
}
```
@bluss would a PR that adds this be accepted?
Yeah, I'd accept this!

Fix patch:
diff --git a/src/lib.rs b/src/lib.rs
index 70816d165..02bca1222 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1541,6 +1541,35 @@ pub trait Itertools : Iterator {
         self.collect()
     }
 
+    /// `.try_collect()` is more convenient way of writing
+    /// `.collect::<Result<_, _>>()`
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// use std::{fs, io};
+    /// use itertools::Itertools;
+    ///
+    /// fn process_dir_entries(entries: &[fs::DirEntry]) {
+    ///     // ...
+    /// }
+    ///
+    /// fn do_stuff() -> std::io::Result<()> {
+    ///     let entries: Vec<_> = fs::read_dir(".")?.try_collect()?;
+    ///     process_dir_entries(&entries);
+    ///
+    ///     Ok(())
+    /// }
+    /// ```
+    #[cfg(feature = "use_std")]
+    fn try_collect<T, U, E>(self) -> Result<U, E>
+    where
+        Self: Sized + Iterator<Item = Result<T, E>>,
+        Result<U, E>: FromIterator<Result<T, E>>,
+    {
+        self.collect()
+    }
+
     /// Assign to each reference in `self` from the `from` iterator,
     /// stopping at the shortest of the two iterators.
     ///


Test patch: