Collect method implementation for Result type

While looking at the last example in the documentation for the collect method (at https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect), I started searching for the implementation of the collect method that makes the collect return the first (and only the first) Err object from a list of Err object (As described at https://doc.rust-lang.org/src/core/result.rs.html#283-288 - see definition for FromIterator method).

Could someone point me where in source is such behavior defined ?

The impl is here, and the function it calls is found here.

The ResultShunt type it uses is defined right above the process_results function.

Ah I misread. The collect function is defined on Iterator and just calls the FromIterator impl directly:

fn collect<B: FromIterator<Self::Item>>(self) -> B
where
    Self: Sized,
{
    FromIterator::from_iter(self)
}

github

Thanks @alice. Seems like process_results has the code that has the logic to select the error if it has an error anywhere in the given iterator (using ResultShunt).

Although I am still learning the language (about half way in the Rust book) I would revisit it once I have covered more ground and this would make much more sense to me. Thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.