title: Idiomatically parsing a whitespace-separated string into a Vec of tuples of differing types
body: <p>I have a struct <code>EnclosingObject</code> which contains a field of a <code>Vec</code> of tuples. I want to implement <code>FromStr</code> for this struct in a way that an <code>EnclosingObject</code> can be parsed from a string with the following structure: <code>&lt;number of tuples&gt; &lt;tuple1 str1&gt; &lt;tuple1 str2&gt; &lt;tuple1 i32&gt; &lt;tuple2 str1&gt; &lt;tuple2 str2&gt;</code> ...</p>
  
  <p>This is what I have come up with so far (ignoring the case of an invalid number of tuples):</p>
  
  <pre><code>use std::str::FromStr;
  use std::num::ParseIntError;
  
  #[derive(Debug)]
  struct EnclosingObject{
      tuples: Vec&lt;(String, String, i32)&gt;,
  }
  
  impl FromStr for EnclosingObject {
      type Err = ParseIntError;
  
      fn from_str(s: &amp;str) -&gt; Result&lt;Self, Self::Err&gt; {
          let elems_vec = s.split_whitespace().collect::&lt;Vec&lt;_&gt;&gt;();
          let mut elems = elems_vec.as_slice();
  
          let num_tuples = elems[0].parse::&lt;usize&gt;()?;
          elems = &amp;elems[1..];
          let mut tuples = Vec::with_capacity(num_tuples);
          for chunk in elems.chunks(3).take(num_tuples){
              tuples.push((chunk[0].into(),
                      chunk[1].into(),
                      chunk[2].parse::&lt;i32&gt;()?));
          }
  
          Ok(EnclosingObject{
              tuples : tuples
          })
      }
  }
  
  fn main(){  
      println!("{:?}", EnclosingObject::from_str("3 a b 42 c d 32 e f 50"));  
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?gist=e34a361ee7750e33bb0ff90c75e9dd52&amp;version=stable" rel="nofollow noreferrer">playground</a>)</p>
  
  <p>As expected, for a valid string it prints out:</p>
  
  <pre class="lang-none prettyprint-override"><code>Ok(EnclosingObject { tuples: [("a", "b", 42), ("c", "d", 32), ("e", "f", 50)] })
  </code></pre>
  
  <p>and for an invalid string e.g. "3 a b x c d 32 e f 50":</p>
  
  <pre class="lang-none prettyprint-override"><code>Err(ParseIntError { kind: InvalidDigit })
  </code></pre>
  
  <p>Can I parse this <code>Vec</code> of tuples in a more elegant/idiomatic way, such as by using iterators?</p>
  
  <p>I tried a combination of <code>map</code> and <code>collect</code>, but the problem with this is the error handling:</p>
  
  <pre><code>let tuples = elems
              .chunks(3)
              .take(num_tuples)
              .map(|chunk| (chunk[0].into(),
                      chunk[1].into(),
                      chunk[2].parse::&lt;i32&gt;()?))
              .collect();
  </code></pre>
  
  <p>The questionmark-operator seems not to work in this context (within the tuple). So I transformed it a bit: </p>
  
  <pre><code>let tuples = try!(elems
              .chunks(3)
              .take(num_tuples)
              .map(|chunk| {
                      let integer = chunk[2].parse::&lt;i32&gt;()?;
                      Ok((chunk[0].into(),
                      chunk[1].into(),
                      integer))})
              .collect());
  </code></pre>
  
  <p>... which works, but again appears a bit cumbersome.</p>