title: How to idiomatically convert from Option<T> to Result<T, ()>?
body: <p>I'm a Rust newbie! What's the best way to convert from an <code>Option&lt;T&gt;</code> to a <code>Result&lt;T, ()&gt;</code>?</p>
  
  <p>The <a href="https://doc.rust-lang.org/std/convert/trait.TryFrom.html" rel="noreferrer">TryFrom</a> trait seems prevalent and returns a Result. The popular num_traits' <a href="https://rust-num.github.io/num/num_traits/cast/trait.NumCast.html" rel="noreferrer">NumCast</a> has many conversions but they all return an <code>Option&lt;T&gt;</code>. Similarly, as do the NonZero* constructors such as <a href="https://doc.rust-lang.org/std/num/struct.NonZeroI32.html" rel="noreferrer">NonZeroI32</a> in the <em>Rust Standard Library</em>. I then noticed that NumCast implements a <code>from()</code> that returns an <code>Option&lt;T&gt;</code> so I was thinking that maybe it had a non-standard way of doing things in general but then I saw the NonZero* implementations and questioned that idea.</p>
  
  <p>Regardless, converting from Options to Results seem frequent and I haven't found a neat way to do yet. E.g.:</p>
  
  <pre class="lang-rust prettyprint-override"><code>/// Add common conversion from an i32 to a non-zero i32.
  impl TryFrom&lt;Container&lt;i32&gt;&gt; for Container&lt;NonZeroI32&gt; {
    type Error = ();
    fn try_from(container: Container&lt;i32&gt;) -&gt; Result&lt;Self, ()&gt; {
      // NonZeroI32::new() returns an Option not a Result. Try a helper.
      Ok(Self(option_to_result(NonZeroI32::new(container.0))?))
    }
  }
  
  /// Helper function to convert from an Option to a Result (both types are
  /// foreign and so is From).
  fn option_to_result&lt;T&gt;(option: Option&lt;T&gt;) -&gt; Result&lt;T, ()&gt; {
    if let Some(some) = option {
      Ok(some)
    } else {
      Err(())
    }
  }
  </code></pre>
  
  <pre class="lang-rust prettyprint-override"><code>/// Add another common conversion from an i32 to an i16.
  impl TryFrom&lt;Container&lt;i32&gt;&gt; for Container&lt;i16&gt; {
    type Error = ();
    fn try_from(container: Container&lt;i32&gt;) -&gt; Result&lt;Self, ()&gt; {
      // NumCast::from() also returns an Option not a Result. Try map_or() instead
      // of the helper.
      Ok(Self(NumCast::from(container.0).map_or(Err(()), |x| Ok(x))?))
    }
  }
  </code></pre>
  
  <p>(Above examples in the <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8342f289b6f43381516353446f723346" rel="noreferrer">Rust Playground</a>.)</p>
  
  <p>These NumCast, NonZero*, and TryFrom conversions seem common enough but my approach feels clumsy as though I'm pitting the Option and Result types against each other. I struggle with these conversions and also miss the fundamental point of the <code>Option</code> type given <code>Result&lt;T,()&gt;</code> feels similar.</p>
  
  <p>So, what's the idiomatic way to convert an <code>Option&lt;T&gt;</code> to <code>Result&lt;T,()&gt;</code> in Rust 2018?</p>