title: How does the Iterator::collect function work?
body: <p>I am trying to understand the full capabilities of <code>collect</code> function by going through <a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html#provided-methods" rel="nofollow noreferrer">some documentation</a>. I am running into some challenges, specifically in the last example quoted on the page (also listed below, with my comments inline)</p>
  <pre><code>let results = [Ok(1), Err(&quot;nope&quot;), Ok(3), Err(&quot;bad&quot;)];
  
  let result: Result&lt;Vec&lt;_&gt;, &amp;str&gt; = results.iter().cloned().collect();
  
  // gives us the first error &lt;-- Point 1
  assert_eq!(Err(&quot;nope&quot;), result);
  
  let results = [Ok(1), Ok(3)];
  
  let result: Result&lt;Vec&lt;_&gt;, &amp;str&gt; = results.iter().cloned().collect();
  
  // gives us the list of answers
  assert_eq!(Ok(vec![1, 3]), result);
  </code></pre>
  <p>I followed up this code with some of my own (shown below)</p>
  <pre><code>let results: [std::result::Result&lt;i32, &amp;str&gt;; 2] = [Err(&quot;nope&quot;), Err(&quot;bad&quot;)];
  
  let result: Vec&lt;Result&lt;i32, &amp;str&gt;&gt; = results.iter().cloned().collect();
  
  // The following prints &lt;-- Point 2
  // &quot;nope&quot;
  // &quot;bad&quot;
  for x in result{
      println!(&quot;{:?}&quot;, x.unwrap_err());
  }
  </code></pre>
  <p>Looking at the implementation of <a href="https://doc.rust-lang.org/src/core/result.rs.html#283-288" rel="nofollow noreferrer">the <code>FromIterator</code> trait on the <code>Result</code> struct</a>, we see that it mentions that &quot;Takes each element in the <code>Iterator</code>: if it is an <code>Err</code>, no further elements are taken, and the <code>Err</code> is returned. Should no <code>Err</code> occur, a container with the values of each <code>Result</code> is returned.</p>
  <p>This explanation is in line with the result that is seen at Point 1 but doesn't seem to work with Point 2. In point 2 I was expecting only &quot;nope&quot; to be printed instead of both values.</p>
  <p>Hence, I am trying to understand where this (selective) conversion is happening and running into a challenge.</p>
  <p>If we look at the method definition itself we see the following.</p>
  <pre><code>#[inline]
  fn from_iter&lt;I: IntoIterator&lt;Item=Result&lt;A, E&gt;&gt;&gt;(iter: I) -&gt; Result&lt;V, E&gt; {
      // FIXME(#11084): This could be replaced with Iterator::scan when this
      // performance bug is closed.
  
      iter::process_results(iter.into_iter(), |i| i.collect())
  }
  </code></pre>
  <p>It shows that the <code>into_iter()</code> method is being called on the iterator. Searching for <code>into_iter</code> gives two implementations</p>
  <pre><code>#[stable(feature = &quot;rust1&quot;, since = &quot;1.0.0&quot;)]
  impl&lt;T, E&gt; IntoIterator for Result&lt;T, E&gt; {
      type Item = T;
      type IntoIter = IntoIter&lt;T&gt;;
  
      /// Returns a consuming iterator over the possibly contained value.
      ///
      /// The iterator yields one value if the result is [`Result::Ok`], otherwise none.
      ///
      /// # Examples
      ///
      /// Basic usage:
      ///
      /// ```
      /// let x: Result&lt;u32, &amp;str&gt; = Ok(5);
      /// let v: Vec&lt;u32&gt; = x.into_iter().collect();
      /// assert_eq!(v, [5]);
      ///
      /// let x: Result&lt;u32, &amp;str&gt; = Err(&quot;nothing!&quot;);
      /// let v: Vec&lt;u32&gt; = x.into_iter().collect();
      /// assert_eq!(v, []);
      /// ```
      #[inline]
      fn into_iter(self) -&gt; IntoIter&lt;T&gt; {
          IntoIter { inner: self.ok() }
      }
  }
  
  #[stable(since = &quot;1.4.0&quot;, feature = &quot;result_iter&quot;)]
  impl&lt;'a, T, E&gt; IntoIterator for &amp;'a Result&lt;T, E&gt; {
      type Item = &amp;'a T;
      type IntoIter = Iter&lt;'a, T&gt;;
  
      fn into_iter(self) -&gt; Iter&lt;'a, T&gt; {
          self.iter()
      }
  }
  </code></pre>
  <p>However, in my limited understanding of the language none seem to be able to explain what the documentation says as well as what is happening in Point 2.</p>
  <p>Could someone please explain how this is working or point me to the right place in source where such selection logic is implemented?</p>
  <p>What I want to understand is not why we get all values in a vector and only one in the result, but a. where is the code/logic to select the first <code>Err</code> from a list of values and b. why are multiple <code>Err</code> values selected when the result is collected in a list (when according to the documentation it should be only the first <code>Err</code> value)</p>