title: How do I write a Rust unit test that ensures that a panic has occurred?
body: <p>I have a Rust function that <code>panic</code>s under some condition and I wish to write a test case to validate whether the function is panicking or not. I couldn't find anything except the <code>assert!</code> and <code>assert_eq!</code> macros. Is there some mechanism for testing this? </p>
  
  <p>I could spawn a new task and checking whether that task panics or not. Does it make sense? </p>
  
  <hr>
  
  <p>Returning a <code>Result&lt;T, E&gt;</code> is not suitable in my case.</p>
  
  <p>I wish to add support for the <code>Add</code> trait to a <code>Matrix</code> type I am implementing. The ideal syntax for such addition would look like:</p>
  
  <pre><code>let m = m1 + m2 + m3;
  </code></pre>
  
  <p>where <code>m1</code>, <code>m2</code>, <code>m3</code> are all matrices. Hence, the result type of <code>add</code> should be <code>Matrix</code>. Something like the following would be too cryptic:</p>
  
  <pre><code>let m = ((m1 + m2).unwrap() + m3).unwrap()
  </code></pre>
  
  <p>At the same time, the <code>add()</code> function needs to validate that the two matrices being added have same dimension. Thus, <code>add()</code> needs to panic if the dimensions don't match.  The available option is <code>panic!()</code>. </p>