title: How to throw error into Result <T, E> without match in Rust?
body: <p>I'm still new to Rust.
  I want to throw an error message into <code>Result&lt;T, E&gt;</code> and it seems to me you can't do that without <code>match</code>. In Javascript you can basically call a <code>callback(error, result)</code> anywhere in the function. What <code>match</code> is doing ? and how to propagate an error with <code>Result&lt;T,E&gt;</code> without <code>match</code>?</p>
  <p>I don't like using <code>panic!</code> as it means the program will shuts off immediately when it encounters an error.</p>
  <p>Thank you in advance.</p>
  <pre><code>use std::io::{Error, ErrorKind}; // im using std::io error here just for example
  
  fn check(array: Vec&lt;usize&gt;) -&gt; Result&lt;bool, Error&gt; {
      let a = array.len();
      // I need to make it error if the array.len() is 3 or above
      match a {
          3 =&gt; Ok(true), 
          _ =&gt; {
              let msg = format!(&quot;Invalid array size!&quot;);
              Err(Error::new(ErrorKind::InvalidData, msg))
          }
      }
  }
  
  fn main(){
      let a = vec!(0, 2, 3, 5);
      let result = check(a);
      let kind = result.map_err(|e| e.kind());
      println!(&quot;error kind: {:?}&quot;, kind);
  }
  </code></pre>
  <p>This doesn't works and prints <code>expected &quot;()&quot;, found enum &quot;std::result::Result&quot;</code></p>
  <pre><code>use std::io::{Error, ErrorKind};
  
  fn check(array: Vec&lt;usize&gt;) -&gt; Result&lt;bool, Error&gt; {
      // if array.len() is 3 or above it throws an error
      if array.len() &gt;= 3 { 
          let msg = format!(&quot;Invalid array size!&quot;);
          Err(Error::new(ErrorKind::InvalidData, msg))
      }
      Ok(true)
  }
  
  fn main(){
      let a = vec!(0, 2, 3, 5);
      let result = check(a);
      let kind = result.map_err(|e| e.kind());
      println!(&quot;error kind: {:?}&quot;, kind);
  }
  </code></pre>
  <p>PLAYGROUND:
  <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=bc9c7add4d5081a591413431738a79f5" rel="nofollow noreferrer">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=bc9c7add4d5081a591413431738a79f5</a></p>