title: Is it possible to express "result 1 if okay or result 2" as a chain of combinators?
body: <p>Imagine I have two functions which return <code>Result</code>s and would like to select the first result if <code>Ok</code> and otherwise the second result. Something like the following:</p>
  <pre class="lang-rust prettyprint-override"><code>fn primary() -&gt; Result&lt;String, ()&gt; {
      Err(())
  }
  
  fn alternative() -&gt; Result&lt;String, ()&gt; {
      Ok(&quot;alternative&quot;.to_string())
  }
  
  fn using_match() -&gt; Result&lt;String, ()&gt; {
      Ok(match primary() {
          Ok(string) =&gt; string,
          _ =&gt; alternative()?
      })
  }
  </code></pre>
  <p>I'm struggling to find a way to express this as a chain of combinators. The following doesn't work for the reason stated in the comment, but it captures the chain structure I'm trying to achieve:</p>
  <pre class="lang-rust prettyprint-override"><code>fn combinator_attempt() -&gt; Result&lt;String, ()&gt; {
      // this is wrong because the closure must return a String
      //                                               v----------------v
      Ok(primary().map(|string| string).unwrap_or_else(|_| alternative()?))
  }
  </code></pre>
  <p>Is there an idiomatic way to express this? For extra points, what is a good way to select the first <code>Ok</code> (or last <code>Err</code>) from a <em>chain</em> of (three or more) possible operations?</p>
  <p>EDIT: In case it helps anyone in the future, my issue here was a failure to think about the problem as, &quot;let's do a series of operations on <code>Result</code>s which yield <code>Result</code>s. Instead, I was trying to unwrap the inner data or otherwise operate on another <code>Result</code>. The key, which I now understand thanks to @stepan's answer, is type continuity along the entire chain until the end.</p>