title: Error handling best-practices
body: <p>I've been fumbling through Rust's documentation trying to execute a simple esoteric example for my own educational benefit more than practicality. While doing this, I can't seem to wrap my head around how Rust's error handling is meant to be used.</p>
  
  <p>The programming example I'm using is to write a function that runs a command in a shell. From the result of the command I want to retrieve <code>stdout</code> (as a <code>String</code> or <code>&amp;str</code>) and know whether or not the command failed.</p>
  
  <p>The <a href="http://doc.rust-lang.org/std/process/struct.Command.html"><code>std::process::Command</code></a> struct gives me the methods I want, but it seems that the only way to combine them is kludgy and awkward:</p>
  
  <pre><code>use std::process::Command;
  use std::string::{String, FromUtf8Error};
  use std::io::Error;
  
  
  enum CmdError {
      UtfError(FromUtf8Error),
      IoError(Error),
  }
  
  
  // I would really like to use std::error::Error instead of CmdError,
  // but the compiler complains about using a trait in this context.
  fn run_cmd(cmd: &amp;str) -&gt; Result&lt;String, CmdError&gt; {
      let cmd_result = Command::new("sh").arg("-c").arg(cmd).output();
  
      match cmd_result {
          Err(e) =&gt; {
              return Err(CmdError::IoError(e));
          }
          Ok(v) =&gt; {
              let out_result = String::from_utf8(v.stdout);
  
              match out_result {
                  Err(e) =&gt; {
                      return Err(CmdError::UtfError(e));
                  }
                  Ok(v) =&gt; {
                      return Ok(v);
                  }
              }
          }
      }
  }
  
  
  fn main() {
      let r = run_cmd("echo 'Hello World!'");
  
      match r {
          Err(e) =&gt; {
              match e {
                  CmdError::IoError(e) =&gt; {
                      panic!("Failed to run command {:}", e);
                  }
                  CmdError::UtfError(e) =&gt; {
                      panic!("Failed to run command {:}", e);
                  }
              }
          }
          Ok(e) =&gt; {
              print!("{:}", e);
          }
      }
  }
  </code></pre>
  
  <p>In particular, the nested match blocks inside <code>run_cmd</code> seem really awkward, and the nested match blocks in <code>main</code> are even worse.</p>
  
  <p>What I'd really like to do is be able to use a more general class of error than <code>FromUtf8Error</code> or <code>io::Error</code> which I can type convert into easily from either concrete type, but it doesn't appear the type system is designed in this way, so I had to use the crude <code>CmdError</code> as somewhat of a <a href="http://en.wikipedia.org/wiki/Union_type">union type</a> instead.</p>
  
  <p>I'm sure there's an easier way to do this which is more idiomatic, but I haven't found it from the documentation I've read so far.</p>
  
  <p>Any <a href="http://xkcd.com/138/">pointers</a> appreciated.</p>